Static Methods
Class-level methods
5 min read
Static Methods in Java
Static methods belong to the class, not instances. They can be called without creating an object using ClassName.methodName().
Static Method Characteristics
- Called on class name, not object
- Cannot access instance variables directly
- Cannot use
thisorsuper - Can only access other static members directly
- Loaded when class is loaded
🔑 Key Rule: Static methods cannot directly access instance members. They need an object reference to do so.
💡 Best Practice: Use static for utility methods that don't depend on object state (Math.sqrt(), Collections.sort()).
Code Examples
Basic static methods and utility classes
java
1public class MathUtils {
2 // Static method - no object needed
3 public static int add(int a, int b) {
4 return a + b;
5 }
6
7 public static int max(int a, int b) {
8 return a > b ? a : b;
9 }
10
11 public static double circleArea(double radius) {
12 return Math.PI * radius * radius;
13 }
14}
15
16// Calling static methods - use class name
17int sum = MathUtils.add(5, 3); // 8
18int bigger = MathUtils.max(10, 7); // 10
19double area = MathUtils.circleArea(5); // 78.54...
20
21// Built-in static methods
22int abs = Math.abs(-10); // 10
23double sqrt = Math.sqrt(16); // 4.0
24int random = (int)(Math.random() * 100);Static methods and variable access rules
java
1public class Counter {
2 private static int count = 0; // Static variable
3 private String name; // Instance variable
4
5 public Counter(String name) {
6 this.name = name;
7 count++;
8 }
9
10 // Static method - can access static variable
11 public static int getCount() {
12 return count;
13 }
14
15 // Static method accessing instance - WRONG!
16 // public static void printName() {
17 // System.out.println(name); // ERROR!
18 // System.out.println(this.name); // ERROR!
19 // }
20
21 // Correct way - pass object reference
22 public static void printName(Counter c) {
23 System.out.println(c.name); // OK!
24 }
25}
26
27Counter c1 = new Counter("First");
28Counter c2 = new Counter("Second");
29System.out.println(Counter.getCount()); // 2Static factory methods pattern
java
1// Factory Method Pattern - common use of static
2public class User {
3 private String name;
4 private int age;
5
6 private User(String name, int age) {
7 this.name = name;
8 this.age = age;
9 }
10
11 // Static factory methods
12 public static User createAdult(String name) {
13 return new User(name, 18);
14 }
15
16 public static User createChild(String name) {
17 return new User(name, 10);
18 }
19
20 public static User fromString(String data) {
21 String[] parts = data.split(",");
22 return new User(parts[0], Integer.parseInt(parts[1]));
23 }
24}
25
26User adult = User.createAdult("Alice");
27User child = User.createChild("Bob");
28User parsed = User.fromString("Charlie,25");Static vs instance access rules
java
1// Static vs Instance methods comparison
2public class Example {
3 private int value;
4 private static int staticValue;
5
6 // Instance method - can access everything
7 public void instanceMethod() {
8 value = 10; // OK - instance var
9 staticValue = 20; // OK - static var
10 staticMethod(); // OK - static method
11 anotherInstance(); // OK - instance method
12 }
13
14 // Static method - limited access
15 public static void staticMethod() {
16 // value = 10; // ERROR - instance var
17 staticValue = 20; // OK - static var
18 // instanceMethod(); // ERROR - instance method
19 anotherStatic(); // OK - static method
20 }
21
22 private void anotherInstance() {}
23 private static void anotherStatic() {}
24}
25
26// main() is static - that's why you can't call
27// instance methods directly from main!
28public static void main(String[] args) {
29 // instanceMethod(); // ERROR!
30 Example e = new Example();
31 e.instanceMethod(); // OK - need object
32}Use Cases
- Utility/helper methods (Math, Collections)
- Factory methods for object creation
- Singleton pattern getInstance()
- main() method (entry point)
- Constants and configuration
- Stateless operations
Common Mistakes to Avoid
- Accessing instance variables from static context
- Using this in static methods
- Overusing static (reduces testability)
- Static methods calling instance methods directly
- Confusing static with final
- Thread safety issues with static variables