Static initialize
by , 01-08-2012 at 10:50 AM (745 Views)
All the code blocks that do not have a function name and are declared as static are executed before the constructor is called. They are usually used to initialize static fields as shown in below code.
The following code gives an Output of A=5, B=10.Java Code: This is an example of static initializepublic class StaticInitilaizer { public static final int A = 5; public static final int B; //Static initializer block, which is executed only once when the class is loaded. static { if(A == 5) B = 10; else B = 5; } public StaticInitilaizer(){} // constructor is called only after static initializer block }










Email Blog Entry
License4J 4.0
Yesterday, 12:23 AM in Java Software