static Keyword in Java
Why do we use static keywords in Java?
- 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
static
keyword is a non-access modifier in Java that is applicable for variables, methods, blocks, and nested classes.
How to create a static member?
To create a static member(block, variable, method, nested class), precede its declaration with the keyword static
.
When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.
// A static member can be accessed before instantiating a class
class Test {
// static method
static void m1() {
System.out.println("from m1");
}
public static void main(String[] args) {
// calling m1 without creating
// any object of class Test
m1();
}
}
Output
from 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.
// The use of static blocks
class Test {
// static variable
static int a = 10;
static int b;
// static block
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
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.
- We can create static variables at the class level only.
static
block andstatic
variables are executed in the order they are present in a program.
// Execution of static blocks and variables
class Test {
// static variable
static int a = m1();
// static block
static {
System.out.println("Inside static block");
}
// static method
static int m1() {
System.out.println("from m1");
return 20;
}
// static method(main !!)
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
orsuper
in any way.
// Restriction on static methods
class Test {
// static variable
static int a = 10;
// instance variable
int b = 20;
// static method
static void m1() {
a = 20;
System.out.println("from m1");
// Cannot make a static reference to the non-static field b
b = 10; // compilation error
// Cannot make a static reference to the
// non-static method m2() from the type Test
m2(); // compilation error
// Cannot use super in a static context
System.out.println(super.a); // compiler error
}
// instance method
void m2() {
System.out.println("from m2");
}
public static void main(String[] args) {
// main method
}
}
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
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.
// Demonstrate use of static keyword with Classes
import java.io.*;
public class GFG {
private static String str = "GeeksforGeeks";
// Static class
static class MyNestedClass {
// non-static method
public void disp() {
System.out.println(str);
}
}
public static void main(String args[]) {
GFG.MyNestedClass obj = new GFG.MyNestedClass();
obj.disp();
}
}
Output
GeeksforGeeks