Thursday, January 04, 2007

Static Block at Class level

There can be static blocks at the class level, which is will do what a constructor will do for a an instantied object. for example if the class has couple of static methods and the class needs to perform few setup tasks before the static methods are executed, since constructor will not be invoked in such cases where a static method is invoked, a static block at the class level (not inside a method) will do the job of a constructor for a static method of the class. NOTE - The Static block is invoked only once for a JVM, so if one static method of a class with static invoked, the static method will be invoked once in that JVM, and when other static methods of the same class invoked in the same JVM, the static block is not invoked again.

No comments: