In Java, methods can be classified as static and non-static (instance) methods. A static method belongs to the class and can be called without creating an object, whereas a non-static method belongs to an object and is generally called using an object of the class.
- Static methods are declared using the static keyword and are associated with the class.
- Non-static methods are associated with objects (instances) of the class.
Static Method
A static method is a method declared using the static keyword. It belongs to the class rather than to individual objects.
- Does not require an object for a direct class-level call.
- Can directly access only static members.
- Cannot directly access instance variables or instance methods.
class Calculator {
// Static method
public static int sum(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
// Calling static method using class name
int result = Calculator.sum(3, 6);
System.out.println("Sum: " + result);
}
}
Output
Sum: 9
Explanation: The sum() method is static, so it belongs to the Calculator class. It can be called using Calculator.sum() without creating a Calculator object.
Non-Static Method
A non-static method, also called an instance method, is a method that belongs to an object of a class.
- Usually called using an object.
- Can also access static variables and static methods.
- Can be overridden in a subclass.
class Calculator {
// Non-static method
public int sum(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
// Creating object
Calculator calculator = new Calculator();
// Calling non-static method using object
int result = calculator.sum(3, 6);
System.out.println("Sum: " + result);
}
}
Output
Sum: 9
Explanation: The sum() method is a non-static method, so it is associated with a Calculator object. Therefore, it is called using the calculator object.
Static Vs Non-Static Method
| Feature | Static Method | Non-Static Method |
|---|---|---|
| Definition | A method associated with the class | A method associated with an object |
| Keyword | Declared using static | Declared without static |
| Calling | Can be called using the class name | Generally called using an object |
| Object Required | No object is required for a direct class-level call | An object is generally required |
| Instance Members | Cannot directly access instance variables or methods | Can directly access instance variables and methods |
| Static Members | Can directly access static variables and methods | Can directly access static variables and methods |
this Reference | Cannot use this | Can use this |
| Binding/Dispatch | Does not use runtime overriding dispatch | Overridden methods use runtime method dispatch |
| Overriding | Cannot be overridden; can be hidden | Can be overridden |
| Polymorphism | Does not support runtime method overriding | Supports runtime polymorphism |
| Memory | Method code is not copied for each object | Method code is not copied for each object |
| Typical Use | Utility or class-level operations | Operations that depend on object state |