Java Static Keyword
The static keyword is used in Java mainly for memory management. Static is a keyword in Java which can be applicable on a variable, method or block and class.
It can be used as –
- Variable (also known as a class variable)
- Method (also known as a class method)
- Block
- Nested class
Java Static Variables
If we declare a variable as static that means it will get memory only once in life, at the time of class loading. A static field is a part of a class and that is not part of the object to the value of the static field is shared among all objects. In other words, only one copy of the static field exists regardless of the member of the class.
Local variables can’t be declared as static.
Syntax
public static double PI = 3.14;
public static String DATABASE_NAME = “ORACLE_SIT”;
Here PI and DATABASE_NAME are the static variables and will get memory only once in life.
Static variables can be accessed by the class name of an object name.
Example of Static Variable
class demo{
int rollno;//instance variable
String name;
static String college ="VMC";//static variable
//constructor
demo(int x, String y){
rollno = x;
name = y;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class StaticTest{
public static void main(String args[]){
demo d1 = new demo(101,"Karan");
demo d2 = new demo(102,"Milan");
d1.display();
d2.display();
}
}
Java Static Method
If we declare a method as static, that means that the method can’t be overridden because the static method is a part of the class rather than an object of the class. It is referred to as the class method so can be called by class name, without creating the object of the class.
A static method can access only static methods or fields but cannot access instance variables.
Example of Static Method
public class Demo {
public static void main(String[] args) {
display();
}
static void display() {
System.out.println("Java is my favorite programming language");
}
}
Java Static Block
We can create a block as static which will be executed at the time of class loading. Static block is mainly used to initialize the class/static variables. The static block is executed before the main method at the time of class loading.
There can be n number of static blocks in a class and they execute in the given order.
Example of Static Block
public class StaticBlock {
public static void main(String[] args) {
System.out.println("The Main method is executed");
}
static {
System.out.println("Static block is executed before the main method.");
}
}
Java Static Class
A class can be a static class only if it is a nested class or inner class.
- Nested static class doesn’t need the reference to execute the operation on the outer class.
- Non-static members of an outer class cannot be accessed by the inner class.
Example of Static Class
public class JavaExample{
private static String str = "Java Point Tutorial";
//Static class
static class MyNestedClass{
//non-static method
public void disp() {
/* If you make the str variable of outer class
* non-static then you will get compilation error
* because: a nested static class cannot access non-
* static members of the outer class.
*/
System.out.println(str);
}
}
public static void main(String args[])
{
/* To create an instance of the nested class we didn't need the outer
* class instance but for a regular nested class you would need
* to create an instance of outer class first
*/
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp();
}
}