As our programs become larger, putting all of our code inside the main() method can quickly become messy. Imagine having a program with hundreds of lines inside one method. Finding, understanding, and reusing that code would become difficult.
This is where methods help us.
A method is a block of code designed to perform a particular task. Instead of writing the same code again and again, we can put it inside a method and call that method whenever we need it.
For example, if we frequently need to print a welcome message, we can create a method for it:
static void sayHello() {
System.out.println("Hello, John!");
}
Then we can call it whenever we want:
sayHello();
Creating a Method
A simple method looks like this:
static void methodName() {
// code
}
For example:
static void greet() {
System.out.println("Welcome to Java!");
}
Here, greet is the name of our method. The code inside the curly braces is what the method will execute when we call it.
We can then call the method from main():
class Main {
static void greet() {
System.out.println("Welcome to Java!");
}
public static void main(String[] args) {
greet();
}
}
Output:
Welcome to Java!
Notice that defining a method does not automatically execute it. The method runs when we call it.
Calling a Method Multiple Times
One of the biggest advantages of methods is that we can reuse them.
class Main {
static void greet() {
System.out.println("Welcome to Java!");
}
public static void main(String[] args) {
greet();
greet();
greet();
}
}
Output:
Welcome to Java!
Welcome to Java!
Welcome to Java!
Instead of writing the System.out.println() statement three times, we wrote it once inside the method and called the method three times.
Methods with Parameters
Sometimes a method needs some information to perform its task. We can provide that information using parameters.
For example, instead of always greeting the same person, we can pass a name:
static void greet(String name) {
System.out.println("Hello, " + name);
}
Now we can call the method with different names:
greet("John");
greet("Jason");
Output:
Hello, John
Hello, Jason
Here, name is the parameter of the method, while "John" and "Jason" are the values we pass to it.
This makes the method much more useful because the same method can work with different data.
Methods That Return a Value
A method doesn't always have to simply perform an action. It can also calculate something and return a result.
For example:
static int add(int a, int b) {
return a + b;
}
We can call the method and store the result:
int result = add(10, 20);
System.out.println(result);
Output:
30
Here, int tells Java that the method will return an integer.
The return statement sends the calculated value back to the place where the method was called.
void Methods
Sometimes a method doesn't need to return anything.
For example:
static void greet() {
System.out.println("Hello!");
}
The keyword void means that this method does not return a value.
Compare the two methods:
static void greet() {
System.out.println("Hello!");
}
and:
static int add(int a, int b) {
return a + b;
}
The first method performs an action but returns nothing. The second performs a calculation and returns an int.
Why Are Methods Useful?
Methods make programs easier to organize and maintain.
Suppose you have a large application that needs to calculate a total price in several different places. Instead of writing the calculation repeatedly, you can create one method:
static double calculateTotal(double price, double tax) {
return price + tax;
}
Then you can call it whenever you need the calculation.
This gives you several benefits: less repeated code, easier debugging, better organization, and easier maintenance.
Methods also make the purpose of your code clearer. A method named calculateTotal() immediately tells us what that part of the program is supposed to do.
A Complete Example
Let's put everything together:
class Main {
static int multiply(int a, int b) {
return a * b;
}
static void showResult(int result) {
System.out.println("Result: " + result);
}
public static void main(String[] args) {
int result = multiply(5, 4);
showResult(result);
}
}
Output:
Result: 20
Here, multiply() performs the calculation and returns the result. Then showResult() displays that result.
The important idea is that each method has a specific responsibility instead of putting everything into one huge block of code.
Methods are one of the most important building blocks in Java. As you continue learning classes and object-oriented programming, you will use methods constantly to define what your program can do.