A method in Java is a block of code that performs a specific task. Methods help organize programs into smaller, reusable units.
- Methods can accept parameters and return values.
- Methods are declared inside classes or interfaces.
public class Geeks {
// User-defined instance method
public void printMessage(){
System.out.println("Hello, Geeks!");
}
public static void main(String[] args){
// Create an object
Geeks obj = new Geeks();
// Call the method
obj.printMessage();
}
}
Output
Hello, Geeks!
Explanation: The printMessage() method prints a message without accepting any parameters or returning a value. It is called using an object of the Geeks class.
Syntax
modifier returnType methodName(parameters) {
// Method body
}
Components of a Method

- Modifier: Specifies the method's accessibility or other properties, such as public or static.
- Return Type: Specifies the type of value returned by the method. Use void if no value is returned.
- Method Name: Identifies the method and is used to call it.
- Parameters: Define the inputs accepted by the method.
- Method Body: Contains the statements executed when the method is called.
The access modifier and parameters are optional. A method declared with a return type other than void must return a compatible value if it completes normally.
Why Use Methods?
- Reduce code duplication by reusing existing logic.
- Divide complex programs into smaller modules.
- Simplify debugging and testing.
- Make code easier to update and maintain.
Types of Methods in Java
Based on their origin, methods can be classified into two types.
1. Predefined Methods
Predefined methods are provided by the Java standard library and can be used directly in a program.
Examples:
Math.sqrt(25);
Math.max(10, 20);
Math.abs(-15);
2. User-Defined Methods
User-defined methods are created by programmers to perform specific tasks based on application requirements.
Example:
int add(int a, int b) {
return a + b;
}
Instance and Static Methods
Java methods can also be classified based on whether they are associated with an object or a class.
- Instance method: Belongs to an object and can access instance and static members of its class.
- Static method: Belongs to a class and can be called without creating an object.
public class Geeks{
// Instance method
int add(int a, int b){
return a + b;
}
// Static method
static int multiply(int a, int b){
return a * b;
}
public static void main(String[] args){
Geeks obj = new Geeks();
// Call instance method
System.out.println(obj.add(10, 20));
// Call static method
System.out.println(Geeks.multiply(10, 20));
// Call predefined method
System.out.println(Math.max(10, 20));
}
}
Output
30 200 20
Explanation:
- obj.add() calls an instance method using an object.
- Geeks.multiply() calls a static method using the class name.
- Math.max() calls a predefined static method.
Method Signature
A method signature consists of the method name and its parameter types in their declared order. Generic methods also include type parameters in their signatures.
Consider the following method:
int add(int a, int b) {
return a + b;
}
The method signature is add(int, int).
The return type, parameter names, access modifiers and declared exceptions are not part of the method signature.
Method Call Stack
The method call stack is a runtime structure used by the JVM to manage method execution. It follows the Last-In-First-Out (LIFO) principle.
- Each method invocation creates a stack frame containing local variables and execution-related information.
- When the method completes, its frame is removed, and control returns to the caller.

Example: Program to demonstrate method call stack execution
public class Geeks{
static void C(){
System.out.println("In Method C");
}
static void B(){
C();
System.out.println("In Method B");
}
static void A(){
B();
System.out.println("In Method A");
}
public static void main(String[] args){
A();
}
}
Output
In Method C In Method B In Method A
Explanation: The main() method calls A(), which calls B(), and then B() calls C(). The methods complete in reverse order, following the LIFO principle.
Naming Conventions for Methods
- Use meaningful names that describe an operation.
- Begin method names with a lowercase letter.
- Follow camelCase for multi-word names, such as calculateSum().
- Methods can share the same name with different parameter lists through method overloading.
These are recommended naming conventions, not mandatory syntax rules.