Method Basics
Methods are the basic building blocks for organizing code. This lesson covers how to define and use methods in Java.
What is a Method
A method is a block of code that performs a specific task and can be called repeatedly.
Benefits of Methods
| Benefit | Description |
|---|---|
| Code reuse | Write once, call many times |
| Clear logic | Break complex problems into smaller methods |
| Easy to maintain | Changes only need to be made in one place |
Method Definition
Syntax
modifier returnType methodName(parameterList) {
// Method body
return returnValue;
}
Example: Simple Method
public class MethodDemo {
// Define method
public static void sayHello() {
System.out.println("Hello, Java!");
}
public static void main(String[] args) {
// Call method
sayHello();
sayHello(); // Can be called multiple times
}
}
Output:
Hello, Java!
Hello, Java!
Method Parameters
Parameters allow methods to receive external data.
No Parameters
public static void printLine() {
System.out.println("================");
}
With Parameters
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
Multiple Parameters
public static int add(int a, int b) {
return a + b;
}
Example: Method Parameters
public class ParamDemo {
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static int add(int a, int b) {
return a + b;
}
public static void printInfo(String name, int age) {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
greet("Alice"); // Hello, Alice!
greet("Bob"); // Hello, Bob!
int sum = add(3, 5);
System.out.println("3 + 5 = " + sum); // 8
printInfo("John", 25); // Name: John, Age: 25
}
}
Return Values
Methods can return a result to the caller.
With Return Value
public static int max(int a, int b) {
return (a > b) ? a : b;
}
No Return Value (void)
public static void printMax(int a, int b) {
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);
}
Example: Return Values
public class ReturnDemo {
public static int max(int a, int b) {
return (a > b) ? a : b;
}
public static double average(int a, int b) {
return (a + b) / 2.0;
}
public static boolean isEven(int num) {
return num % 2 == 0;
}
public static void main(String[] args) {
System.out.println("max(3, 5) = " + max(3, 5)); // 5
System.out.println("avg(3, 5) = " + average(3, 5)); // 4.0
System.out.println("isEven(4) = " + isEven(4)); // true
System.out.println("isEven(7) = " + isEven(7)); // false
}
}
return value, but can use return; to exit the method early.
Method Overloading
Method overloading means having multiple methods with the same name but different parameter lists in the same class.
Overloading Rules
| Rule | Description |
|---|---|
| Same method name | Required |
| Different number of parameters | Allowed |
| Different parameter types | Allowed |
| Different parameter order | Allowed |
| Different return type | Not considered overloading |
Example: Method Overloading
public class OverloadDemo {
public static int add(int a, int b) {
return a + b;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
public static double add(double a, double b) {
return a + b;
}
public static String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(1, 2)); // 3
System.out.println(add(1, 2, 3)); // 6
System.out.println(add(1.5, 2.5)); // 4.0
System.out.println(add("Hello", "World")); // HelloWorld
}
}
Varargs (Variable Arguments)
Varargs allow methods to accept a variable number of arguments.
Syntax
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
Example: Varargs
public class VarargsDemo {
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
public static void printNames(String... names) {
for (String name : names) {
System.out.println(name);
}
}
public static void main(String[] args) {
System.out.println(sum(1, 2)); // 3
System.out.println(sum(1, 2, 3)); // 6
System.out.println(sum(1, 2, 3, 4, 5)); // 15
printNames("Alice", "Bob", "Charlie");
}
}
Parameter Passing
Java uses "pass by value" for parameter passing.
Primitive Type Passing
public static void changeValue(int num) {
num = 100; // Only modifies local variable
}
public static void main(String[] args) {
int x = 10;
changeValue(x);
System.out.println(x); // 10, unchanged
}
Reference Type Passing
public static void changeArray(int[] arr) {
arr[0] = 100; // Modifies array content
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
changeArray(arr);
System.out.println(arr[0]); // 100, modified
}
Method Call Stack
When methods are called, stack frames are created in stack memory.
public static void main(String[] args) {
methodA();
}
public static void methodA() {
methodB();
}
public static void methodB() {
System.out.println("Hello");
}
Call order: main → methodA → methodB → completes → returns to methodA → returns to main
❓ Frequently Asked Questions
📖 Summary
- Methods are the basic units for organizing code and can be called repeatedly
- Methods can have parameters and return values
- Method overloading: same method name, different parameter lists
- Varargs allow methods to accept a variable number of arguments
- Java uses pass by value
📝 Exercises
- Method practice: Write methods to calculate the maximum, minimum, and average of two numbers
- Overloading practice: Write multiple print methods that support different parameter types
- Varargs: Write a method that calculates the average of any number of values
Next Lesson
In the next lesson, we'll learn about Advanced Methods — recursion, static methods, and method references.



