Defining Methods
in Java
Learn to create reusable blocks of code by defining methods with parameters and return types to write cleaner, more organized programs.
What Is a Method?
A method is a named block of code that performs a specific task. Instead of writing the same logic over and over, you write it once inside a method, then call that method whenever you need it.
Anatomy of a Method
Why Methods Matter
Imagine you need to calculate the area of a rectangle in ten different places in your program. Without methods, you'd copy-paste the same formula ten times. With a method, you write it once and call it ten times.
The Method Signature
A method's signature is the combination of its name and parameter list. Java uses the signature to distinguish between methods, even if they share the same name (this is called overloading).
public static int add(int a, int b) {
return a + b;
}
// Signature: add(double, double)
public static double add(double a, double b) {
return a + b;
}
Return Types: void vs Value
public static void greet(String name) {
System.out.println("Hello, " + name);
}
// int: computes and returns a value
public static int square(int n) {
return n * n;
}
Parameters vs Arguments
These two terms are often confused, but they refer to different things. Parameters are the placeholders in the method definition. Arguments are the actual values you pass when calling the method.
How to Define a Method
Example 1: A Simple void Method
for (int i = 0; i < length; i++) {
System.out.print("-");
}
System.out.println();
}
// Calling it:
printLine(20); // prints: --------------------
Example 2: A Value-Returning Method
return Math.PI * radius * radius;
}
// Calling it and using the result:
double a = circleArea(5.0);
System.out.println(a); // 78.539...
Example 3: Multiple Parameters
return "Hello, " + title + " " + name
+ ". You are " + age + " years old.";
}
// Call:
String msg = buildGreeting("Dr.", "Smith", 45);
// "Hello, Dr. Smith. You are 45 years old."
How a Method Call Works
Parameter Scope
Parameters and variables declared inside a method only exist within that method's curly braces. This is called local scope. Once the method finishes, those variables are destroyed.
int result = x * 2; // result exists HERE only
return result;
}
// In main():
int y = doubleIt(5); // y = 10
// System.out.println(result); // ERROR! result doesn't exist here
Common Mistakes
Comparing Method Types
| void Methods | Value-Returning Methods | |
|---|---|---|
| Return type | void | int, double, String, etc. |
| return statement | Optional (or return;) | Required with a value |
| Can store result? | No | Yes: int x = method(); |
| Use case | Print, modify state | Compute and return data |
| Example | printLine(20); |
double area = circleArea(5.0); |
Question 1 of 5
Which of the following is a valid method header in Java?
Question 2 of 5
What does the keyword void indicate in a method declaration?
Question 3 of 5
Consider this method:
return (x + y) / 2.0;
}
What is the result of calling average(7, 4)?
Question 4 of 5
Consider this code:
if (a > b) {
return a;
}
return b;
}
What does the method mystery do?
Question 5 of 5
Consider this code:
int result = triple(4);
System.out.println(result);
}
public static int triple(int n) {
int value = n * 3;
}
What happens when this code is compiled?