- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java





- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


Java Class Static Methods
In Java, a static method belongs to the class, not to instances of the class. This means you can call a static method without creating an object of the class. If you've ever used
Math.sqrt()
or Integer.parseInt()
, you've already used static methods.
Static methods are useful when the behavior doesn’t depend on instance data (i.e., member variables). They're ideal for utility or helper operations.
How to Define a Static Method
To define a static method, use thestatic
keyword before the return type:
public class MathUtils {
public static int square(int x) {
return x * x;
}
}
You can call this method directly using the class name:
int result = MathUtils.square(5);
System.out.println(result);
25
Static Method vs Instance Method
- Static methods don’t need an object to be called. - Instance methods require an object to be created.public class Hello {
public static void sayStaticHello() {
System.out.println("Hello from static!");
}
public void sayInstanceHello() {
System.out.println("Hello from instance!");
}
public static void main(String[] args) {
// Calling static method
Hello.sayStaticHello();
// Calling instance method
Hello h = new Hello();
h.sayInstanceHello();
}
}
Hello from static!
Hello from instance!
When to Use Static Methods
Use static methods when:- The method logic does not rely on object state.
- You are writing utility/helper methods.
- You need global access to common behavior.
Accessing Static Methods from Another Class
public class Greet {
public static void sayHi() {
System.out.println("Hi there!");
}
}
public class Main {
public static void main(String[] args) {
Greet.sayHi();
}
}
Hi there!
Calling Static Methods from Instance Methods
Static methods can be called from instance methods without any restriction:public class Demo {
public static void showStatic() {
System.out.println("Static method called");
}
public void showInstance() {
System.out.println("Calling from instance method...");
showStatic(); // or Demo.showStatic();
}
public static void main(String[] args) {
new Demo().showInstance();
}
}
Calling from instance method...
Static method called
Limitations of Static Methods
Static methods have some boundaries:- They cannot access non-static (instance) variables directly.
- They cannot use
this
orsuper
. - They are not polymorphic — cannot be overridden like instance methods.
public class Test {
int x = 100;
public static void display() {
// System.out.println(x); // Compile-time error
System.out.println("Static method can't access instance variables directly");
}
}
Static Block vs Static Method
- A static block is executed once when the class is loaded.
- A static method can be called multiple times explicitly.
public class Example {
static {
System.out.println("Static block executed");
}
public static void greet() {
System.out.println("Static method executed");
}
public static void main(String[] args) {
Example.greet();
Example.greet();
}
}
Static block executed
Static method executed
Static method executed
Best Practices for Static Methods
- Keep them stateless — don’t rely on global data.
- Use them for shared utilities or mathematical operations.
- Avoid overusing static methods in OOP-heavy designs.
Common Examples in Java API
Math.abs()
,Math.max()
Collections.sort()
Integer.parseInt()
Thread.sleep()
These are all static — no need to instantiate the class.