Static Method in Java With Examples
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:
- 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.
Syntax to Declare a 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 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 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();
}
}
Output
40 50 40
Example 2: Static Methods Accessed from Both Static and Non-Static Methods
// 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();
}
}
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.
Difference Between the Static Method and Instance Method
Instance 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. |