static ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-09-25

Static Methods in Java and the static Keyword

Write a plain method greet(), call it from main(), and javac refuses to compile: error: non-static method greet() cannot be referenced from a static context. The reason is that main() is itself a static method — it runs without any object, so there is no instance on which greet() could be called.

A static method in Java is a method declared with the static keyword. It belongs to the class itself rather than to any object, so it can be called without creating an instance, using the class name: ClassName.method(). That is the key difference from an instance (non-static) method, which is always called on a specific object.

How to declare and call a static method

To declare a static method, add the static modifier before the return type. The recommended way to call it is through the class name; inside the same class the name can be omitted.

public class StaticMethodClass {
    static int staticVar = 3;
    int nonStaticVar;

    public void nonStaticMethod() {
        System.out.println("Instance method");
    }

    static void staticMethod(int localVar) {
        System.out.println("localVar = " + localVar);
        System.out.println("staticVar = " + staticVar);
        // Compile error: an instance field cannot be used
        // from a static method without an object reference
        // System.out.println("nonStaticVar = " + nonStaticVar);
    }

    public static void main(String[] args) {
        staticMethod(42);                   // same class: the class name can be omitted
        StaticMethodClass.staticMethod(42); // the same call, explicitly through the class name

        // Compile error: non-static method nonStaticMethod()
        // cannot be referenced from a static context
        // nonStaticMethod();

        StaticMethodClass obj = new StaticMethodClass();
        obj.nonStaticMethod();              // instance method - called on an object
        obj.staticMethod(67);               // compiles, but is bad style
    }
}

From another class, a static method is called through the class name — no object is needed:

public class StaticMethodExample {
    public static void main(String[] args) {
        StaticMethodClass.staticMethod(42);
    }
}

Code style

The call obj.staticMethod(67) compiles, but it suggests that the method depends on the object, which is false. IntelliJ IDEA flags it, and javac -Xlint:static warns that the static method should be qualified by type name. Always call static methods through the class name: StaticMethodClass.staticMethod(67).

What a static method can and cannot access

A static method runs without an object, so it has no this. Several rules follow from that:

  • Directly (without an object reference), a static method can call only other static methods of its class.
  • Directly, it can use only static variables of the class, plus its own parameters and local variables.
  • The keywords this and super cannot be used inside a static method.
  • Instance fields and instance methods are reachable only through an explicit object reference, like obj.nonStaticMethod() in the example above.

The reverse restriction does not exist: an instance method can freely call static methods and read static fields of its class.

Fixing "non-static method cannot be referenced from a static context"

Can a static method call a non-static method? Yes, but only through an object reference. Calling an instance method by its bare name from a static method is the most common compile error Java beginners see:

public class Greeter {
    private String name = "World";

    void greet() {
        System.out.println("Hello, " + name);
    }

    public static void main(String[] args) {
        greet(); // error: non-static method greet() cannot be referenced from a static context
    }
}

There are two ways to fix it, and the right one depends on what the method does:

  • Create an object and call the method on it — the correct fix when the method uses instance state, as greet() uses the field name: new Greeter().greet();
  • Make the method static — appropriate only if the method does not touch instance fields. If you simply add static to greet() here, the error just moves: non-static variable name cannot be referenced from a static context.
public static void main(String[] args) {
    Greeter greeter = new Greeter();
    greeter.greet(); // Hello, World
}

Static vs instance methods in Java

Characteristic Static method Instance method
Belongs to The class A specific object
How it is called ClassName.method() object.method()
Needs an object No Yes
Access to this and super No Yes
Access to fields Static fields only (others through an object reference) Both static and instance fields
Inheritance Hidden in a subclass (method hiding), not overridden Overridden in a subclass, polymorphism works
Which implementation runs Chosen at compile time, by the reference type Chosen at runtime, by the actual object type

Static methods and inheritance: hiding, not overriding

If a subclass declares a static method with the same signature as its parent, the new method hides the parent's method instead of overriding it. The version that runs depends on the declared type of the reference, not on the object:

class Parent {
    static String name() { return "Parent"; }
}

class Child extends Parent {
    static String name() { return "Child"; } // hides Parent.name()
}

public class HidingExample {
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(p.name());     // Parent - resolved by the reference type
        System.out.println(Child.name()); // Child
    }
}

Putting @Override on Child.name() causes a compile error, because there is nothing to override.

When to use static methods

A static method fits when its result depends only on its parameters, not on the state of an object. Typical cases:

  • Utility methods — Math.max(a, b), Math.sqrt(x), Arrays.sort(arr), Integer.parseInt("42"), Collections.emptyList().
  • Static factory methods that create objects instead of a constructor — List.of(1, 2, 3), LocalDate.of(2024, 1, 1), String.valueOf(10).
  • The program entry point — public static void main(String[] args).
  • Working with static fields — counters, caches, shared configuration.

If a method reads or changes an object's fields, make it an instance method. Overusing static turns object-oriented code into procedural code and makes testing harder: a static call is difficult to replace with a mock.

Static methods in records

A record may declare static methods and static fields (unlike additional instance fields, which are not allowed outside the record components). The most common use is a static factory method or a predefined constant:

public record Point(int x, int y) {
    static final Point ORIGIN = new Point(0, 0);

    static Point of(int x, int y) {
        return new Point(x, y);
    }
}

Point p = Point.of(3, 4);

Good to know

A record declared inside another class is implicitly static, just like nested interfaces and enums. Writing static record Point(...) is allowed but redundant.

Frequently Asked Questions

Can you overload a static method in Java?

Yes. Overloading only requires a different parameter list, so a class can have static int sum(int a, int b) and static double sum(double a, double b) side by side — Math.max is overloaded this way. What static methods cannot do is be overridden: a subclass method with the same signature only hides the parent's one.

Why is the main method static in Java?

The JVM starts a program before any object of your class exists. A static main can be invoked through the class name without creating an instance, which is why it serves as the entry point.

What happens if you call a static method through a null reference?

No NullPointerException is thrown. The compiler binds the call to the declared type of the variable, not to the object, so StaticMethodClass obj = null; obj.staticMethod(1); runs normally. It is a popular interview question and one more reason to call static methods only through the class name.

Can a static method be abstract?

No. An abstract method must be overridden in a subclass, and static methods cannot be overridden, so abstract static is rejected with the compile error "illegal combination of modifiers".

Are static methods thread-safe?

The static modifier by itself gives no thread safety. A method that uses only its parameters and local variables is safe. If it modifies static fields shared by all threads, it needs synchronization: for example, static synchronized locks on the Class object of the class, or you can use atomic types such as AtomicInteger.

Comments

Please log in or register to have a possibility to add comment.