Declaring static Methods
What you'll learn: How to write methods that belong to the class itself rather than to any specific object.
The Core Idea
Just like static fields are shared at the class level, static methods are behaviors that belong to the class rather than to individual instances. You declare them using the static keyword, and they can be called using the class name without creating an object.
Syntax for Declaring static Methods
The syntax is straightforward—place static between the access modifier and the return type:
public static returnType methodName(parameters) {
// method body
}
A Concrete Example
public class Calculator {
// A static method to add two numbers
public static int add(int a, int b) {
return a + b;
}
// A static method to multiply two numbers
public static int multiply(int a, int b) {
return a * b;
}
}
// Calling static methods using the class name
int sum = Calculator.add(5, 3); // 8
int product = Calculator.multiply(4, 7); // 28
Notice you didn't need to write new Calculator() first—the methods are called directly on the class.
Think of It This Way
If a regular method is like a skill each person (object) has individually, a static method is like a public utility everyone can use. You don't need to own a copy of the fire department to call 911; similarly, you don't need an instance to call a static method.
Important Constraint
Static methods can only access other static members (static fields and static methods). They cannot access instance fields or instance methods directly, because there's no specific object to pull that data from.
Key Takeaway: Declare a method as static when its behavior doesn't depend on any particular object's state—it becomes a class-level utility that can be called using the class name.