Java no name static method

AEIOU picture AEIOU · Dec 2, 2009 · Viewed 8.6k times · Source

What is this?

public class ABC {
   public ABC() {
         System.out.println("world");
   }
   static {
         System.out.println("hello");
   }
}

Will print: hello world

I don't really understand this, or what kind of method that static code is.

Answer

Noon Silk picture Noon Silk · Dec 2, 2009

It's called a "static initialisation block".

It runs when the class is first loaded; only once.

For example, a constructor will run each time the class is instantiated; the static block only runs once, when it's first loaded statically by the VM/Class loader.