Java Class Methods
In Java, methods define the behavior of a class. While attributes (fields) represent the state, methods provide the actions that objects can perform.
Understanding class methods is essential for writing modular, reusable, and object-oriented code.
In this guide, we’ll cover:
- What Java methods are
- Difference between instance and static methods
- Method overloading vs overriding
- Best practices
- Examples with code
What Are Java Methods?
- A method is a block of code that performs a specific task.
- Methods may take parameters (inputs) and may return a value (output).
- Syntax:
returnType methodName(parameters) {
// method body
return value;
}
Types of Class Methods
- Instance Methods
- Belong to an object.
- Can access instance variables and other instance methods.
- Need an object of the class to be called.
- Static Methods
- Declared using the static keyword.
- Belong to the class, not to objects.
- Can be called without creating an object.
- Useful for utility functions or operations not tied to object state.
Method Overloading
- Same method name, but different parameter lists (type, number, or order).
- Happens at compile time (Compile-time Polymorphism).
Example:
public class MathUtils {
// Overloaded add() methods
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
public int add(int a, int b, int c) { return a + b + c; }
}
Method Overriding
- A subclass provides its own implementation of a method already defined in a superclass.
- Happens at runtime (Runtime Polymorphism).
- Must have the same name, parameters, and return type.
- Use @Override annotation for clarity.
Example:
class Animal {
public void sound() {
System.out.println(“Animal makes a sound”);
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println(“Dog barks”);
}
}
Example: Java Class Methods
public class Calculator {
// Static method
public static int add(int a, int b) {
return a + b;
}
// Instance method
public int multiply(int a, int b) {
return a * b;
}
// Main method for testing
public static void main(String[] args) {
// Calling static method
int sum = Calculator.add(5, 3);
System.out.println(“Sum: ” + sum);
// Calling instance method
Calculator calc = new Calculator();
int product = calc.multiply(4, 6);
System.out.println(“Product: ” + product);
}
}
Output:
Sum: 8
Product: 24
Best Practices for Methods
- Keep methods short and focused (single responsibility principle).
- Use meaningful names (calculateTax, findStudentById).
- Use static for utility methods.
Avoid too many parameters – use objects if needed. - Document methods with JavaDoc for clarity.
Conclusion
Java methods define how objects behave and interact. By mastering instance methods, static methods, method overloading, and overriding, you’ll write cleaner and more reusable code.
Remember:
- Use instance methods when behavior depends on object state.
- Use static methods for utility operations that don’t rely on object data.