Java static Keyword

    static keyword

    Java uses the static keyword for memory management which can be used with any variable, method, or class. To declare any class, block, or variable as static, we have to use the static keyword before that. Static variable refers to the common property of any object. For example- many students belong to the same college.

    Static keyword allows you to manage the memory in an efficient manner. If we consider a number of students for the same college, then instead of allocating memory for the college variable every time during object creation, it is a better option to use that variable as static, that will be used as the common property for all student objects.

    Example-

    class Student{  
         int rollno;  
         String name;  
         String college="ITS";  
    }  

    Instead of using the above example, we can declare the college variable as static.

    class Student{  
       String name;
       static String college ="ITS";//static variable  
    
       Student(String n)
       {  
           name = n;  
       }  
    
       void display ()
       {
          System.out.println("+name+" "+college);
       }  
    }  
    
    
    
    public class StaticVar{  
    
     public static void main(String args[]){  
    
     Student s1 = new Student("Jacob");  
    
     Student s2 = new Student("Sam");  
    
     s1.display();  
    
     s2.display();  
    
     }  
    
    }  

    Output-

    Jacob ITS
    Sam ITS

    Counter example with and without static variable

    Without static

    With static

    class Counter{  
       int count=0; //memory will be allocated everytime with object creation
       
       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

    class Counter{  
         int count=0; //memory will be allocated only once with object creation
           
         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
    2
    3

    If we use a static keyword the memory will be allocated only once, and the value will be used from that memory location.

    Static method in Java

    You can create a static method using the static keyword. A static method always belongs to the class instead of the class object. You can directly invoke the static method without creating the object of the class. Only a static method can use and change the value of the static data member.

    Example-

    class Student{  
    
       String name;
    
       static String college ="ITS";//static variable  
    
       Student(String n){  
    
       name = n;  
    
       }  
    
    
    
    Static void Demo()
    
    {
    
    college=”AB”;
    
    }
    
       void display (){System.out.println("+name+" "+college);}  
    
    }  
    
    
    
    public class StaticVar{  
    
     public static void main(String args[]){  
    
     Student s1 = new Student("Jacob");  
    
     Student s2 = new Student("Sam");  
    
     s1.display();  
    
     s2.display();  
    
     }  
    
    }

    Output-

    Jacob AB
    Sam AB

    Static method limitation

    There are below limitations when you may face working with the static methods-

    • You cannot use the non-static data member or can call a non-static method from a static method.
    • Static will not support the use of this and super keywords.

    Example-

    class A{  
       int a=40;//non static  variable
       public static void main(String args[]){  
       System.out.println(a);  
     }  
    }        

    Output-

    Compile-time error

    Static block in Java

    • You can use the static block to initialize the static data member.
    • This block will get executed before the main method during class loading.
    • You can even execute the program without having the main method using the static block.

    Example-

    class Demo{  
    
      static{System.out.println("Static block");}  
    
      public static void main(String args[]){  
    
       System.out.println("Main method");  
    
      }  
    
    }  

    Output-

    Static block
    
    Main method