01 October 2010

Static versus instance initializers blocks of a java class

class Foo {

       /* Static initializer: executes when the class is loaded. */
       static {
              System.out.println("class Foo loaded!");
       }

       /* Instance initializer: excutes when calling new Foo(),
       BEFORE anything inside the Foo constructor */
       {
              System.out.println("new instance of Foo about to be created!");
              // has access to internal Foo methods:
              print("instance initializer has access to internal methods!");
       }

       /* Constructor */
       public Foo() {
              System.out.println("Foo constructor statements!");
       }

       public void print(String msg) {
              System.out.println(msg);
       }


       static public void main(String[] args) {
              new Foo();
              System.out.println(" ");
              new Foo();
       }
}

No comments:

Post a Comment