Hey there! Just getting started with Java and wondering what people mean when they say “methods”? You’re not alone.
Methods are one of the most important parts of Java—and once you understand them, writing code will feel a whole lot easier.
In this guide, I’ll explain methods in simple terms, show you some real examples, and help you see why they’re such a useful part of your coding toolbox.
🤔 So, What Is a Method?
Think of a method like a mini task or action in your program.
It’s a chunk of code that only runs when you ask it to. You can “call” a method to perform a specific job—like printing a message, adding two numbers, or checking a user’s login.
🧠 Everyday Example
Let’s say you’re baking cookies. Instead of writing down the whole recipe every time, you just say “use the cookie recipe.” That’s like calling a method. The recipe is written once and reused.
In programming, that’s exactly what a method does: write it once, reuse it when needed.
📌 Why Do We Use Methods?
Here’s why methods are awesome (and why pros use them all the time):
- ✅ They keep your code neat and organized
- 🔁 You don’t have to repeat the same code again and again
- 🛠 If something changes, you only update it in one place
- 🧩 They help break big problems into smaller, easy parts
- 🧠 They make your code easier to read and understand
✍️ How to Create a Method in Java
Creating a method in Java isn’t hard. Here's the basic structure:
returnType methodName(parameters) {
// code to run
}
Let’s break it down:
returnType
: What the method gives back (like a number, text, or nothing)methodName
: A name for your methodparameters
: Optional input the method uses (like ingredients for a recipe)- The code inside
{}
runs when the method is called
🧪 Example 1: A Simple Method
public class Main {
static void sayHello() {
System.out.println("Hello there! Welcome to Java.");
}
public static void main(String[] args) {
sayHello(); // Calling the method
}
}
Output:
Hello there! Welcome to Java.
What’s happening?
- We created a method called
sayHello()
- We called it inside the
main()
method - It prints a welcome message when run
➕ Example 2: Method That Adds Numbers
Let’s write a method that takes two numbers and returns their sum.
public class Main {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 10);
System.out.println("The sum is: " + result);
}
}
Output:
The sum is: 15
Why is this useful?
- You can call
add()
any time you need to add two numbers. - You don’t need to rewrite the logic—it’s already handled by the method.
🎯 Example 3: Method with Input from User
import java.util.Scanner;
public class Main {
static void greetUser(String name) {
System.out.println("Hi " + name + "! Glad to see you learning Java.");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String userName = scanner.nextLine();
greetUser(userName); // Passing user input to method
}
}
Output (example):
Enter your name: Sam
Hi Sam! Glad to see you learning Java.
This method makes your program interactive by taking input from users.
🧠 Key Things to Know About Methods
- A method name should be short and make sense (
printMessage
,calculateArea
, etc.) - If a method doesn’t return anything, use
void
- If it returns something (like an int or String), use that as the return type
- You can call a method just by writing its name and giving it any needed inputs
- You can have multiple methods in a class
📈 Next-Level Stuff (Once You're Comfortable)
When you’ve got the basics down, you can start exploring:
- 🔄 Method Overloading: Same method name, different inputs
- ♻️ Recursion: A method that calls itself
- 🔐 Access Modifiers: Who can use your methods (like
public
,private
) - 👥 Static vs. Instance Methods: When to use each one
📚 Want to Learn More?
Here are some great places to keep exploring:
✅ Wrapping Up
Methods are a huge part of Java—and once you understand them, you’ll start writing cleaner, smarter code. They save time, reduce mistakes, and help you think like a real programmer.
Start simple, try writing your own methods, and play around with them. The more you practice, the more natural it will feel.
Happy coding! 🚀
Posted on May 12, 2025