Method and Types of Methods
In Java, a method is a block of code that performs a specific task. Methods are used to organize code, improve reusability, and reduce duplication. Instead of writing the same logic multiple times, a method allows you to define it once and reuse it wherever required.
Methods are one of the most important concepts in Java programming and are heavily used in both small programs and large enterprise applications.
What is a Method?
A method is a group of statements that perform a particular function. It is defined inside a class and can be called using its name.
Example:
Here, greet() is a method.
Why Methods are Important
- Reduce code duplication
- Improve readability and maintainability
- Allow modular programming
- Make debugging easier
- Encourage code reuse
Syntax of a Method
Example
Explanation
int→ return typeadd→ method name(int a, int b)→ parametersreturn a + b;→ return statement
Parts of a Method
- Return Type: type of value returned (e.g., int, void)
- Method Name: identifier used to call the method
- Parameters: inputs to the method
- Method Body: actual logic
- Return Statement: sends result back to caller
Method Without Return Value (void)
A method with void return type does not return any value.
Method With Return Value
A method with a return type returns a value using the return statement.
Calling a Method
A method is executed when it is called.
Types of Methods in Java
Methods can be classified based on parameters and return values:
- No parameters, no return value
- Parameters, no return value
- No parameters, return value
- Parameters and return value
1. No Parameters, No Return Value
2. Parameters, No Return Value
3. No Parameters, Return Value
4. Parameters and Return Value
Method Overloading
Method overloading means having multiple methods with the same name but different parameter lists.
This improves flexibility and readability.
Static Methods
Static methods belong to the class and can be called without creating an object.
Instance Methods
Instance methods belong to objects and require object creation.
Method with Arguments Example
Method Call by Value
Java uses call by value, meaning a copy of the value is passed to the method.
Output remains 10 because original value is not changed.
Recursive Methods
A method that calls itself is called a recursive method.
Varargs Method
Varargs allow passing variable number of arguments.
Method Execution Flow
When a method is called:
- Control moves to method
- Method executes statements
- Returns result (if any)
- Control goes back to caller
Common Mistakes
- Forgetting return statement in non-void methods
- Wrong parameter types
- Confusing static and instance methods
- Infinite recursion without base condition
Best Practices
- Use meaningful method names
- Keep methods small and focused
- Avoid too many parameters
- Use return values instead of printing inside methods
- Follow naming conventions (camelCase)
Interview-Oriented Points
- A method is a reusable block of code
- Java supports method overloading
- Java uses call by value
- Static methods belong to class
- Instance methods belong to object
- Recursive methods call themselves
Conclusion
Methods are essential for writing clean, modular, and reusable code in Java. Understanding different types of methods and how they work helps in building scalable applications.
From simple utility methods to complex business logic, methods form the backbone of Java programming.

