Wednesday, June 15, 2011

static block

Static block resembles a method with no name/arguments/return type. There is no need to refer to it from outside the class definition, therefore it doesn’t need a name. If it doesn’t have any name, who is executing this block? Simple :) This block is executed by the Java VM when the class is loaded.

Why Do I Need a Static Block

Static blocks are very useful if you consider doing things like below:

  • If you’re loading drivers and other items into the namespace. (for instanceClass.forName(”org.h2.Driver”))
  • Initialize your static members at once (most likely this would be singletons, etc)
  • Security related issues or logging related tasks
  • Some database operations like creating prepared SQL statements, etc
Execution Order

Java doesn’t limit you with one static block. So you may define more than one static block. JVM combines them together and then executes. Here you might think what about the order of all these statements? Since static code blocks could be separated by non-static code blocks, the execution might not be in the same order you see in the code. You must remember that JVM combines all these blocks into one single static block and then executes. Here are some notes from my understanding:

  • If you’re defining static variables/methods, JVM will execute these statements when they need.
  • If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM.
  • If you’re referring some static variables/methods from the static blocks, these statements will be executed after the class is loaded into JVM (same time as second).

 

   1:  package certify;
   2:   
   3:  public class StaticBlock {
   4:   
   5:      public static final String name = "Static Variable Name";
   6:   
   7:      {
   8:          System.out.println("Instance Initialization Block");
   9:      }
  10:      static {
  11:          System.out.println("Static Block 1");
  12:          show();
  13:      }
  14:      public StaticBlock() {
  15:          System.out.println("CONSTRUCTOR");
  16:      }
  17:      public static void show() {
  18:          System.out.println("Static Method");
  19:      }
  20:      public static void main(String[] args) {
  21:          System.out.println("MAIN");
  22:          new StaticBlock();
  23:          new StaticBlock();
  24:      }
  25:      static {
  26:          System.out.println("STATIC BLOCK 2");
  27:          System.out.println("Static Block Name = " + name);
  28:      }
  29:  }



Output


Static Block 1
Static Method
STATIC BLOCK 2
Static Block Name = Static Variable Name
MAIN
Instance Initialization Block
CONSTRUCTOR
Instance Initialization Block
CONSTRUCTOR


So what happened in above code? Let me try to explain. First of all, Java didn’t do anything before the class is loaded into JVM. After loading, JVM combined all the static blocks into one regardless of their position in the code, and started to execute each line. If line is executable JVM executed that line, otherwise it just kept the method/variable blocks on a special area in the memory. And when they are needed, JVM executed those statements too. If you double check, we have 2 static methods, one is show and the other is main. Show is executed as the first item since it is called within the static block. However, main is executed after all static initializations.


One thing you might notice is there is another block without static keyword. And it’s been executed twice. Yes, that block is not a static block. It is instance initialization block.


Limitations

Yes, we have couple limitations about static blocks:


  • You shouldn’t try to access super since there is no such a thing for static blocks
  • You cannot use this keyword since there is no instance :)
  • You cannot throw Checked Exceptions
  • And please don’t try to return anything from this block

No comments:

Post a Comment