The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. The static keyword is used for a constant variable or a method that is the same for every instance of a class.
The static keyword is a non-access modifier in Java that is applicable for the following:
- Blocks
- Variables
- Methods
- Classes
Note: To create a static member(block, variable, method, nested class), precede its declaration with the keyword static.
Characteristics of static keyword:
Here are some characteristics of the static keyword in Java:
- Shared memory allocation: Static variables and methods are allocated memory space only once during the execution of the program. This memory space is shared among all instances of the class, which makes static members useful for maintaining global state or shared functionality.
- Accessible without object instantiation: Static members can be accessed without the need to create an instance of the class. This makes them useful for providing utility functions and constants that can be used across the entire program.
- Associated with class, not objects: Static members are associated with the class, not with individual objects. This means that changes to a static member are reflected in all instances of the class, and that you can access static members using the class name rather than an object reference.
- Cannot access non-static members: Static methods and variables cannot access non-static members of a class, as they are not associated with any particular instance of the class.
- Can be overloaded, but not overridden: Static methods can be overloaded, which means that you can define multiple methods with the same name but different parameters. However, they cannot be overridden, as they are associated with the class rather than with a particular instance of the class.
When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. For example, in the below java program, we are accessing static method m1() without creating any object of the Test class.
Java
class Test
{
static void m1()
{
System.out.println( "from m1" );
}
public static void main(String[] args)
{
m1();
}
}
|
Static blocks
If you need to do the computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded.
Consider the following java program demonstrating the use of static blocks.
Java
class Test
{
static int a = 10 ;
static int b;
static {
System.out.println( "Static block initialized." );
b = a * 4 ;
}
public static void main(String[] args)
{
System.out.println( "from main" );
System.out.println( "Value of a : " +a);
System.out.println( "Value of b : " +b);
}
}
|
Output
Static block initialized.
from main
Value of a : 10
Value of b : 40
For a detailed article on static blocks, see static blocks
Static variables
When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.
Important points for static variables:
- We can create static variables at the class level only. See here
- static block and static variables are executed in the order they are present in a program.
Below is the Java program to demonstrate that static block and static variables are executed in the order they are present in a program.
Java
class Test
{
static int a = m1();
static {
System.out.println( "Inside static block" );
}
static int m1() {
System.out.println( "from m1" );
return 20 ;
}
public static void main(String[] args)
{
System.out.println( "Value of a : " +a);
System.out.println( "from main" );
}
}
|
Output
from m1
Inside static block
Value of a : 20
from main
Static methods
When a method is declared with the static keyword, it is known as the static method. The most common example of a static method is the main( ) method. As discussed above, Any static member can be accessed before any objects of its class are created, and without reference to any object. Methods declared as static have several restrictions:
- They can only directly call other static methods.
- They can only directly access static data.
- They cannot refer to this or super in any way.
Below is the java program to demonstrate restrictions on static methods.
Java
class Test
{
static int a = 10 ;
int b = 20 ;
static void m1()
{
a = 20 ;
System.out.println( "from m1" );
b = 10 ;
m2();
System.out.println( super .a);
}
void m2()
{
System.out.println( "from m2" );
}
public static void main(String[] args)
{
}
}
|
Output:
prog.java:18: error: non-static variable b cannot be referenced from a static context
b = 10; // compilation error
^
prog.java:22: error: non-static method m2() cannot be referenced from a static context
m2(); // compilation error
^
prog.java:25: error: non-static variable super cannot be referenced from a static context
System.out.println(super.a); // compiler error
^
prog.java:25: error: cannot find symbol
System.out.println(super.a); // compiler error
^
symbol: variable a
4 errors
When to use static variables and methods?
Use the static variable for the property that is common to all objects. For example, in class Student, all students share the same college name. Use static methods for changing static variables.
Consider the following java program, that illustrates the use of static keywords with variables and methods.
Java
class Student {
String name;
int rollNo;
static String cllgName;
static int counter = 0 ;
public Student(String name)
{
this .name = name;
this .rollNo = setRollNo();
}
static int setRollNo()
{
counter++;
return counter;
}
static void setCllg(String name) { cllgName = name; }
void getStudentInfo()
{
System.out.println( "name : " + this .name);
System.out.println( "rollNo : " + this .rollNo);
System.out.println( "cllgName : " + cllgName);
}
}
public class StaticDemo {
public static void main(String[] args)
{
Student.setCllg( "XYZ" );
Student s1 = new Student( "Alice" );
Student s2 = new Student( "Bob" );
s1.getStudentInfo();
s2.getStudentInfo();
}
}
|
Output
name : Alice
rollNo : 1
cllgName : XYZ
name : Bob
rollNo : 2
cllgName : XYZ

Static Classes
A class can be made static only if it is a nested class. We cannot declare a top-level class with a static modifier but can declare nested classes as static. Such types of classes are called Nested static classes. Nested static class doesn’t need a reference of Outer class. In this case, a static class cannot access non-static members of the Outer class.
Note: For static nested class, see a static nested class in java
Implementation:
Java
import java.io.*;
public class GFG {
private static String str = "GeeksforGeeks" ;
static class MyNestedClass {
public void disp(){
System.out.println(str);
}
}
public static void main(String args[])
{
GFG.MyNestedClass obj
= new GFG.MyNestedClass();
obj.disp();
}
}
|
Here’s an example Java program that demonstrates the use of the static keyword:
Java
public class ExampleClass {
public static int count = 0 ;
public int id;
public ExampleClass() {
count++;
id = count;
}
public static void printCount() {
System.out.println( "Number of instances: " + count);
}
public void printId() {
System.out.println( "Instance ID: " + id);
}
public static void main(String[] args) {
ExampleClass e1 = new ExampleClass();
ExampleClass e2 = new ExampleClass();
ExampleClass e3 = new ExampleClass();
e1.printId();
e2.printId();
e3.printId();
ExampleClass.printCount();
}
}
|
Output
Instance ID: 1
Instance ID: 2
Instance ID: 3
Number of instances: 3
Advantages:
- Memory efficiency: Static members are allocated memory only once during the execution of the program, which can result in significant memory savings for large programs.
- Improved performance: Because static members are associated with the class rather than with individual instances, they can be accessed more quickly and efficiently than non-static members.
- Global accessibility: Static members can be accessed from anywhere in the program, regardless of whether an instance of the class has been created.
- Encapsulation of utility methods: Static methods can be used to encapsulate utility functions that don’t require any state information from an object. This can improve code organization and make it easier to reuse utility functions across multiple classes.
- Constants: Static final variables can be used to define constants that are shared across the entire program.
- Class-level functionality: Static methods can be used to define class-level functionality that doesn’t require any state information from an object, such as factory methods or helper functions.
Overall, the static keyword is a powerful tool that can help to improve the efficiency and organization of your Java programs.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!