Static Method in Java With Examples

Last Updated : 8 Apr, 2026

In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method.

  • A static method in Java is associated with the class, not with any object or instance.
  • It can be accessed by all instances of the class, but it does not rely on any specific instance.
  • Static methods can access static variables directly without the need for an object.
  • They cannot access non-static variables (instance) or methods directly.
  • Static methods can be accessed directly in both static and non-static contexts.

Declare Syntax

access_modifier static return_type methodName() {

// method body

}

The name of the class can be used to invoke or access static methods.

Syntax to Call a Static Method

ClassName.methodName();

Example 1: Static Method Cannot Access Instance Variables

The JVM executes the static method first, even before creating class objects. So, static methods cannot access instance variables, as no object exists at that point.

Java
import java.io.*;

public class Geeks {
    
    // static variable
    static int a = 40;

    // instance variable
    int b = 50;

    void simpleDisplay()
    {
        System.out.println(a);
        System.out.println(b);
    }

    // Declaration of a static method
    static void staticDisplay()
    { 
      System.out.println(a); 
    }

    // main method
    public static void main(String[] args)
    {
        Geeks obj = new Geeks();
        obj.simpleDisplay();

        // Calling static method
        staticDisplay();
    }
}

Output
40
50
40

Example 2: Static Methods Accessed from Both Static and Non-Static Methods

Java
import java.io.*;

public class Geeks {
  
    static int num = 100;
    static String str = "GeeksForGeeks";

    // This is Static method
    static void display()
    {
        System.out.println("Static number is: " + num);
        System.out.println("Static string is: " + str);
    }

    // non-static method
    void nonstatic()
    {
        // our static method can accessed 
        // in non static method
        display();
    }

    // main method
    public static void main(String args[])
    {
        Geeks obj = new Geeks();
      
        // This is object to call non static method
        obj.nonstatic();
      
        // static method can called 
        // directly without an object
        display();
    }
}

Output
Static number is: 100
Static string is: GeeksForGeeks
Static number is: 100
Static string is: GeeksForGeeks

Why Use Static Methods?

  • To access or modify static variables or perform actions not tied to any instance.
  • Useful for utility or helper classes like Collections, Math, etc.

Restrictions on Static Methods

  • Non-static data members or non-static methods cannot be used by static methods, and static methods cannot call non-static methods directly.
  • In a static environment, this and super are not allowed to be used.

Why is the main Method Static in Java?

The main method must be static because the JVM does not create an object of the class before invoking it. If it were a non-static method, JVM would first build an object before calling the main() method, resulting in an extra memory allocation difficulty.

Static Method Vs Instance Method

Instance MethodsStatic Methods
Requires an object of the classDoes not require an object
Can access both instance and static variablesCan access only static variables directly
Called using object reference (obj.methodName())Called using class name (ClassName.methodName())
Can call both static and instance methodsCan call only static methods directly
Associated with object (instance level)Associated with class (class level)
Can use this and super keywordsCannot use this and super
Used for object-specific behaviorUsed for common/shared behavior
Java uses pass-by-valueJava uses pass-by-value
Comment