Methods in Java
Writing Reusable and Maintainable Code
Imagine you’re building an e-commerce application.
Whenever an order is placed, you need to:
Validate the order
Calculate the total amount
Send a confirmation email
Now imagine this logic is repeated in 20 different places.
Your code would quickly become difficult to maintain.
This is exactly why Methods exist.
Methods allow us to write a piece of logic once and reuse it whenever needed.
What is a Method?
A Method is a block of code that performs a specific task.
Instead of writing the same code repeatedly, we place it inside a method and call it whenever required.
Example:
public static void greet() {
System.out.println("Welcome to ByteCraft!");
}
Calling the method:
greet();
Output:
Welcome to ByteCraft!
Why Do We Need Methods?
Without methods:
System.out.println("Order Processed");
System.out.println("Order Processed");
System.out.println("Order Processed");
With methods:
public static void processOrder() {
System.out.println("Order Processed");
}
processOrder();
processOrder();
processOrder();
Benefits:
✅ Reusability
✅ Cleaner Code
✅ Easier Maintenance
✅ Better Testing
Method Structure
returnType methodName(parameters) {
// method body
}
Example:
public static void greet() {
System.out.println("Hello Java");
}
Components:
public→ Access Modifierstatic→ Belongs to Classvoid→ No Return Valuegreet→ Method Name
Methods with Parameters
Parameters allow methods to work with dynamic data.
public static void greet(String name) {
System.out.println("Hello " + name);
}
Calling:
greet("Gaurav");
Output:
Hello Gaurav
Methods with Return Values
Methods can return data.
public static int add(int a, int b) {
return a + b;
}
Calling:
int result = add(10, 20);
Output:
30
Real-World Backend Example
Order Total Calculation:
public static double calculateTotal(
double price,
int quantity) {
return price * quantity;
}
Usage:
double total =
calculateTotal(499.99, 2);
Output:
999.98
This type of logic is commonly placed inside service classes in Spring Boot applications.
Method vs Function
This is a very common interview question.
Function
A Function is a reusable block of code that exists independently.
Languages like C, JavaScript, and Python have standalone functions.
Example (JavaScript):
function greet() {
console.log("Hello");
}
Method
A Method is a function that belongs to a class.
Example:
public class UserService {
public void createUser() {
System.out.println("User Created");
}
}
Here:
createUser()
belongs to:
UserService
Therefore it is called a Method.
Why Java Uses Methods Instead of Functions
Java follows Object-Oriented Programming (OOP).
Everything is organized around classes and objects.
Because of this design:
Java does not support standalone functions.
Every executable block must belong to a class.
Therefore Java uses Methods.
Interview Questions
1. What is a Method?
A Method is a reusable block of code that performs a specific task.
2. Why do we use Methods?
To avoid code duplication and improve maintainability.
3. Can a Method return a value?
Yes.
Example:
public int add(int a, int b) {
return a + b;
}
4. What is the difference between a Method and a Function?
A Function exists independently.
A Method belongs to a class.
5. Does Java support standalone Functions?
No.
Java uses Methods because it follows Object-Oriented Programming principles.
Key Takeaways
✅ Methods help eliminate duplicate code.
✅ Methods improve readability and maintainability.
✅ Methods can accept parameters.
✅ Methods can return values.
✅ In Java, every method belongs to a class.
✅ A Method is essentially a Function associated with a class.
In the next blog, we’ll explore:
Method Overloading in Java: How One Method Can Perform Multiple Tasks

