The static keyword is used to construct methods that will exist regardless of whether or not any instances of the class are generated. Any method that uses the static keyword is referred to as a static method.
Features of static method:
- A static method in Java is a method that is part of a class rather than an instance of that class.
- Every instance of a class has access to the method.
- Static methods have access to class variables (static variables) without using the class’s object (instance).
- Only static data may be accessed by a static method. It is unable to access data that is not static (instance variables).
- In both static and non-static methods, static methods can be accessed directly.
Syntax to declare the static method:
Access_modifier static void 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: The static method does not have access to the instance variable
The JVM runs the static method first, followed by the creation of class instances. Because no objects are accessible when the static method is used. A static method does not have access to instance variables. As a result, a static method can’t access a class’s instance variable.
Java
import java.io.*;
public class GFG {
static int a = 40 ;
int b = 50 ;
void simpleDisplay()
{
System.out.println(a);
System.out.println(b);
}
static void staticDisplay()
{
System.out.println(a);
}
public static void main(String[] args)
{
GFG obj = new GFG();
obj.simpleDisplay();
staticDisplay();
}
}
|
Example 2: In both static and non-static methods, static methods are directly accessed.
Java
import java.io.*;
public class StaticExample {
static int num = 100 ;
static String str = "GeeksForGeeks" ;
static void display()
{
System.out.println( "static number is " + num);
System.out.println( "static string is " + str);
}
void nonstatic()
{
display();
}
public static void main(String args[])
{
StaticExample obj = new StaticExample();
obj.nonstatic();
display();
}
}
|
Outputstatic number is 100
static string is GeeksForGeeks
static number is 100
static string is GeeksForGeeks
Why use Static Methods?
- To access and change static variables and other non-object-based static methods.
- Utility and assist classes frequently employ static methods.
Restrictions in 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 aren’t allowed to be used.
Why is the main method in Java static?
It’s because calling a static method isn’t needed of the object. If it were a non-static function, 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 doesn’t 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. |