«Back to Home

Core Java

Topics

Static Keyword In Java

Static Keyword
 
Static is a non-access modifier in Java. A static keyword is a member of a class rather than instance of a class. This is mainly used for memory management in Java. Static keyword can apply with variables, methods, blocks, and nested class.
 
Static variable

In Java, when we use static keyword with any variable that variable called as a static variable. It is used to refer the common property of all objects and in class area at the time of class loading it gets memory only once. It helps to saves the memory. It is also used to retain the value.
 
Let’s see an example of static variable.
 
Code
  1. public class Employee {  
  2.     int empId;  
  3.     String name;  
  4.     static String company = "MNC Company";  
  5.     Employee(int e, String n) {  
  6.         empId = e;  
  7.         name = n;  
  8.     }  
  9.     void displayInfo() {  
  10.         System.out.println(empId + " " + name + " " + company);  
  11.     }  
  12.     public static void main(String args[]) {  
  13.         Employee e1 = new Employee(101"Ash");  
  14.         Employee e2 = new Employee(102"John");  
  15.         Employee e3 = new Employee(103"Mariya");  
  16.         e1.displayInfo();  
  17.         e2.displayInfo();  
  18.         e3.displayInfo();  
  19.   
  20.     }  
  21. }  


Output

b

In the above example, the entire employees have different empId and name. But company name refers to the common property of all objects that’s why we use static keyword. Because static gets memory only one time and helps to saves the memory.
 
Static method

In Java, when we use static keyword with any method that method called as a static method. Static method is also a member of a class than object of a class. Static method can invoked without creating an object of a class. Static method can directly access static data members and we can change the value of it with the use of static.
 
Limitations of static method
  • We cannot use this and super in static framework.
  • Static method cannot use non-static data member.
  • Static method cannot call directly non-static method.
Let’s see an example of static method.
 
Code
  1. public class Employee {  
  2.     int empId;  
  3.     String name;  
  4.     static String company = "MNC Company";  
  5.     static void change() {  
  6.         company = "SAS Company";  
  7.     }  
  8.     Employee(int e, String n) {  
  9.         empId = e;  
  10.         name = n;  
  11.     }  
  12.     void displayInfo() {  
  13.         System.out.println(empId + " " + name + " " + company);  
  14.     }  
  15.     public static void main(String args[]) {  
  16.         Employee e1 = new Employee(125"Tom");  
  17.         Employee e2 = new Employee(263"Dick");  
  18.         Employee.change();  
  19.         e1.displayInfo();  
  20.         e2.displayInfo();  
  21.     }  
  22. }  
c

Output

d

In Java main method is static. Why?

In Java main method is static because object is not necessary to call a static method. If main method is non static method than may be it creates a problem. JVM first create an instance of a class than call the main method and it may be gets extra memory allocation.
 
Static block

Static block mainly used to initialize the static data member and at runtime, it is executed before main method.
 
Let’s see an example of static block.
 
Code
  1. public class StaticBlockEx {  
  2.     static {  
  3.         System.out.println("Hello,I am Static block");  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         System.out.println("Hello,I am main method");  
  7.     }  
  8. }  
e

Output

f

We can execute a program without main method with the use of static block. But (before JDK 1.7) only in previous version of JDK not in new version.

Summary

Thus, we learned that a static keyword is a member of a class rather than instance of a class. This is mainly used for memory management in Java and also learns how we can use it in Java.