Static keyword in Java

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
The static keyword can be:
  • variable (also known as class variable)
  • method (also known as class method)
  • block
  • nested class
  • static variable in java

    If you declare any variable as static, it is known static variable.
  • The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • The static variable gets memory only once in class area at the time of class loading.
  • Advantage of static variable

    It makes your program memory efficient (i.e it saves memory).

    problem without static variable:

    class Student{  
    int rollno;  
    String name;  
    String college="PES";  
       } 
    
    Suppose there are 1000 students in my college, now all instance data members will get memory each time when object is created.All student have its unique rollno and name so instance data member is good.Here, college refers to the common property of all objects.If we make it static,this field will get memory only once.

    Example:

    class Student3 {  
    int rollno;  
    String name;  
    static String college ="PES";  
    Student3(int r,String n)  {  
    rollno = r;  
    name = n;  
       }  
    void display () {
    System.out.println(rollno+" "+name+" "+college);
       }  
    public static void main(String args[]) {  
    Student3 s1 = new Student3(110,"Rahul");  
    Student3 s2 = new Student3(220,"Raman");  
    s1.display();  
    s2.display();  
      }  
      }
    

    Output:

    110   Rahul   PES
    220   Raman   PES
    

    Program of counter without static variable

    we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.

    Example:

    class Counter  {  
    int count=0;    //will get memory when instance is created  
    Counter()  {  
    count++;  
    System.out.println(count);  
      }  
    public static void main(String args[]){  
    Counter c1 = new Counter();  
    Counter c2 = new Counter();  
    Counter c3 = new Counter();  
      }  
      }  
    

    Output:

     1
     1
     1
    

    Program of counter by static variable

    As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.

    Example:

    class Counter1 {  
    static int count=0; //will get memory only once and retain its value  
    Counter1()  {  
    count++;  
    System.out.println(count);  
      }  
    public static void main(String args[]) {  
    Counter1 c1=new Counter1();  
    Counter1 c2=new Counter1();  
    Counter1 c3=new Counter1();  
      }  
      } 
    

    Output:

    1
    2
    3
    

    static method in java

    you apply static keyword with any method, it is known as static method.
  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.
  • Example:

    class Student4 {  
    int rollno;  
    String name;  
    static String college = "PES";  
    static void change() {  
    college = "IIMT";  
     }  
    Student4(int r, String n) {  
    rollno = r;  
    name = n;  
      }  
    void display ()  { 
    System.out.println(rollno+" "+name+" "+college);}  
    public static void main(String args[]) {  
    Student4.change();  
    Student4 s1 = new Student4(110,"Rahul");  
    Student4 s2 = new Student4 (220,"Raman");  
    Student4 s3 = new Student4 (330,"Amit");  
    s1.display();  
    s2.display();  
    s3.display();  
      }  
      } 
    

    Output:

    111  Rahul    IIMT
    222  Raman  IIMT
    330  Amit      IIMT
    

    static block in java

  • Is used to initialize the static data member.
  • It is executed before main method at the time of classloading.
  • Example of static block

    class P2{  
    static{
    System.out.println("static block is invoked");
     }  
    public static void main(String args[]){  
    System.out.println("Catchmecoder");  
      }  
      }
    

    Output:

    static block is invoked
       Catchmecoder