Methods
A method in Java is a reusable block of code defined inside a class. Every method has a unique signature (name + parameters), a return type, and can have access modifiers (public, private, protected). Unlike Python, which uses def, Java uses more formal syntax.
Method overloading allows multiple methods with the same name but different parameters within one class. Varargs (Type... name) allows a variable number of parameters. Java 8+ supports lambda expressions for functional interfaces — a compact way to create anonymous functions.
Methods in Java can be static (belong to the class, no object required) or instance (belong to the object). Private methods (Java 9+) can only be called from within the same class, helping reduce duplication of internal code.
Method Components:
- Access modifiers:
public,private,protected, default (package) - Non-access modifiers:
static,final,abstract,synchronized - Return type:
voidif it returns no value - Parameter list:
(Type name, Type name) - Varargs:
Type... namefor a variable number of parameters
Code Example
public class JavaMethods {
// Static method
static int add(int a, int b) {
return a + b;
}
// Method overloading
static double add(double a, double b) {
return a + b;
}
// Varargs
static int total(int... numbers) {
int sum = 0;
for (int a : numbers) sum += a;
return sum;
}
// Method with a default parameter (via overloading)
static String greet(String name) {
return greet(name, "Hello");
}
static String greet(String name, String greeting) {
return greeting + ", " + name + "!";
}
// Lambda (Java 8+)
interface Function {
int calculate(int x);
}
public static void main(String[] args) {
System.out.println(add(5, 3)); // 8
System.out.println(add(2.5, 3.5)); // 6.0
System.out.println(total(1, 2, 3, 4)); // 10
System.out.println(greet("John")); // Hello, John!
System.out.println(greet("Anna", "Good morning")); // Good morning, Anna!
// Lambda
Function square = x -> x * x;
System.out.println("Square of 5: " + square.calculate(5));
}
}
Code Explanation
- Static method:
static int add(int a, int b)— can be called without creating an object, justJavaMethods.add(5, 3). - Method overloading: two methods named
add— one forint, one fordouble. Java picks the appropriate one based on the argument types at the call site. - Varargs:
int... numbersaccepts zero or moreintarguments, which are packaged into anint[]array. - Default parameter pattern: because Java does not support default parameters directly, use overloading — the method without the second parameter calls the method with the default value.
- Lambda:
x -> x * xis a lambda expression for the functional interfaceFunction. More compact than an anonymous class.
Everyday Example
A calculator program with various operations using overloading:
public class Kalkulator {
static double calculate(String operation, double... numbers) {
return switch (operation) {
case "add" -> {
double sum = 0;
for (double a : numbers) sum += a;
yield sum;
}
case "multiply" -> {
double result = 1;
for (double a : numbers) result *= a;
yield result;
}
case "average" -> {
double sum = 0;
for (double a : numbers) sum += a;
yield sum / numbers.length;
}
default -> throw new IllegalArgumentException("Unknown operation");
};
}
public static void main(String[] args) {
System.out.println("Add: " + calculate("add", 10, 20, 30));
System.out.println("Multiply: " + calculate("multiply", 2, 3, 4));
System.out.println("Average: " + calculate("average", 80, 90, 85, 70));
}
}
Summary
Methods are the basic unit of execution in Java. Overloading provides flexibility without changing method names. Lambda expressions (Java 8+) bring concise functional programming. Always state access modifiers explicitly — do not rely on default package access.