Home > DeveloperSection > Blogs > Java static keyword

Java static keyword


Java Java 
Ratings:
0 Comment(s)
 695  View(s)
Rate this:
It is used for memory management in Java. he following are the java static keywords which are used i.e.,
>variable(class variable)
>method(class method)
>block
>nested class
JAVA STATIC VARIABLE:
If you declare any variable as static , it is known as static variable. Let's understand more elaborately about static variables,
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.
EXAMPLE OF STATIC VARIABLE
  1. class Student8{  
  2.    int rollno;  
  3.    String name;  
  4.    static String college ="ITS";  
  5.      
  6.    Student8(int r,String n){  
  7.    rollno = r;  
  8.    name = n;  
  9.    }  
  10.  void display (){System.out.println(rollno+" "+name+" "+college);}  
  11.   
  12.  public static void main(String args[]){  
  13.  Student8 s1 = new Student8(111,"Karan");  
  14.  Student8 s2 = new Student8(222,"Aryan");  
  15.    
  16.  s1.display();  
  17.  s2.display();  
  18.  }  
  19. }  
  20. Output:111 Karan ITS
           222 Aryan ITS
  21. PROGRAM OF COUNTER WITH STATIC VARIABLE
Static variable will get memory only once,if any object changes the value of static variable it will retain its value.


  1. class Counter2{  
  2. static int count=0;//will get memory only once and retain its value  
  3.   
  4. Counter2(){  
  5. count++;  
  6. System.out.println(count);  
  7. }  
  8.   
  9. public static void main(String args[]){  
  10.   
  11. Counter2 c1=new Counter2();  
  12. Counter2 c2=new Counter2();  
  13. Counter2 c3=new Counter2();  
  14.   
  15.  }  
  16. }  
Output:1
       2
       3
JAVA STATIC METHOD

If 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 of static method

  1. class Student9{  
  2.      int rollno;  
  3.      String name;  
  4.      static String college = "ITS";  
  5.        
  6.      static void change(){  
  7.      college = "BBDIT";  
  8.      }  
  9.   
  10.      Student9(int r, String n){  
  11.      rollno = r;  
  12.      name = n;  
  13.      }  
  14.   
  15.      void display (){System.out.println(rollno+" "+name+" "+college);}  
  16.   
  17.     public static void main(String args[]){  
  18.     Student9.change();  
  19.   
  20.     Student9 s1 = new Student9 (111,"Karan");  
  21.     Student9 s2 = new Student9 (222,"Aryan");  
  22.     Student9 s3 = new Student9 (333,"Sonoo");  
  23.   
  24.     s1.display();  
  25.     s2.display();  
  26.     s3.display();  
  27.     }  
  28. }  
Output:111 Karan BBDIT
       222 Aryan BBDIT
       333 Sonoo BBDIT
JAVA STATIC BLOCK
Is used to initialize the static data member.
It is executed before main method at the time of classloading.
Example of static block:
  1. class A2{  
  2.   static{System.out.println("static block is invoked");}  
  3.   public static void main(String args[]){  
  4.    System.out.println("Hello main");  
  5.   }  
  6. }  
  7. Output:static block is invoked
           Hello main
    
  8. Now, question arise why java main method is static?
  9. It is because object is not required to call static method if it were non-static method, JVM create object first then call main() method that will lead the problem of extra memory allocation.


Don't want to miss updates? Please click the below button!

Follow MindStick