» » » Static block in Java

  • Static variables are class level variables and without instantiating class we can access these variables.

    1. Static int a;


  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Now its time to discuss about static blocks.
  • Static block also known as static initializer
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }

     

What is the need of static block?

  • Static blocks will be executed at the time of class loading.
  • So if you want any logic that needs to be executed at the time of class loading that logic need to place inside the static block so that it will be executed at the time of class loading. 
  • To initialize the static variables while class loading itself we need static block.
  • If we have multiple classes then if you want to see the order of those classes loading then static block needed.

When to use static blocks?

  • If you want to access static variables before executing the constructor.
  • If you want to execute your code only once even any number of objects created.
  • If you want to initialize static constants.

When and where static blocks will be executed?

  • Static blocks will be executed at the time of class loading by the JVM  by creating separate stack frames in java stacks area (Please refer JVM Architecture  to know about stacks area).
  • Static blocks will be executed in the order they defined from top to bottom.



  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

Order of execution of static block and main method:

  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8.         System.out.println("Main method executed");
  9. }
  10.  
  11. static{
  12.       System.out.println("First static block executed");
  13. }
  14.  
  15. static{
  16.      System.out.println("Second static block executed");
  17. }
  18.  
  19. static{
  20.      System.out.println("Third static block executed");
  21. }
  22.  
  23. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed
  4. Main method executed


Order of execution of Static block and constructor:

 

  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. StaticBlockDemo(){
  7.  
  8.   System.out.println("Constructor executed");
  9.  
  10. }
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13.  
  14.     System.out.println("Main method executed");
  15.     StaticBlockDemo obj = new StaticBlockDemo();
  16.  
  17. }
  18.  
  19. static{
  20.       System.out.println("First static block executed");
  21. }
  22.  
  23. static{
  24.      System.out.println("Second static block executed");
  25. }
  26.  
  27. static{
  28.      System.out.println("Third static block executed");
  29. }
  30.  
  31. }
     


Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed
  4. Main method executed
  5. Constructor executed

What is the output of following program:


  1. package com.instanceofjava;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6.   static int a=getNum();

  7. public static int getNum(){
  8.  
  9. System.out.println("In method m1()");
  10. return 10;
  11.  
  12. }

  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16.     System.out.println("Main method executed");
  17.   
  18.  
  19. }
  20.  
  21. static{
  22.       System.out.println("First static block executed");
  23. }
  24.  
  25. static{
  26.      System.out.println("Second static block executed");
  27. }
  28.  
  29. static{
  30.      System.out.println("Third static block executed");
  31. }
  32.  
  33. }
     
Output:
  1. In method m1()
  2. First static block executed
  3. Second static block executed
  4. Third static block executed
  5. Main method executed


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu