import java.util.Random;
class Initable{
static final int staticFinal = 47;
static final int staticFinal2 = ClassInitialization.rand.nextInt(1000);
static{
System.out.println("Initializing Initable");
}
}
class Initable2{
static int staticNonFinal = 147;
static{
System.out.println("Initializing Initable2");
}
}
class Initable3{
static int staticNonFinal = 74;
static{
System.out.println("Initializing Initable3");
}
}
public class ClassInitialization{
public static Random rand = new Random(47);
public static void main (String[] args) throws Exception{
Class initable = Initable.class;
splitLine("Initable");
System.out.println(Initable.staticFinal);
System.out.println(Initable.staticFinal2);
splitLine(null);
System.out.println(Initable2.staticNonFinal);
splitLine("Initable2");
Class initable3 = Class.forName("Initable3");
splitLine("Initable3");
System.out.println(Initable3.staticNonFinal);
}
public static void splitLine(String desc){
if(null!= desc) System.out.println("After creating "+desc+" ref");
System.out.println("------------------------------------");
}
* output:
*
* After creating Initable ref
* ------------------------------------
* 47
* Initializing Initable
* 258
* ------------------------------------
* Initializing Initable2
* 147
* After creating Initable2 ref
* ------------------------------------
* Initializing Initable3
* After creating Initable3 ref
* ------------------------------------
* 74
*/
}