Static Method in Java With Examples (original) (raw)

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.

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();
}

}

`

**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?

Restrictions on Static Methods

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 Methods Static Methods
Requires an object of the class Does not require an object
Can access both instance and static variables Can access only static variables directly
Called using object reference (obj.methodName()) Called using class name (ClassName.methodName())
Can call both static and instance methods Can call only static methods directly
Associated with object (instance level) Associated with class (class level)
Can use this and super keywords Cannot use this and super
Used for object-specific behavior Used for common/shared behavior
Java uses pass-by-value Java uses pass-by-value