Last Updated : 02 May, 2025
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.
Features of Static Method:
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 MethodExample 1: Static Method Cannot Access Instance VariablesClassName.methodName();
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
// Java program to demonstrate that
// the static method does not have
// access to the instance variable
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
// Java program to demonstrate that
// in both static and non-static methods,
// static methods are directly accessed
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();
}
}
Static number is: 100 Static string is: GeeksForGeeks Static number is: 100 Static string is: GeeksForGeeksWhy Use Static Methods?
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.
Difference Between the Static Method and Instance MethodInstance Methods
Static Methods
It requires an object of the class. It does not require an object of the class. It can access all attributes of a class. It can access only the static attribute of a class. The methods can be accessed only using object reference. The method is only accessed by class name. Syntax: Objref.methodname() Syntax: className.methodname() It's an example of pass-by-value programming. It is an example of pass-by-reference programming.Static Method in Java With Examples
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4