Initially I thought I would have a chicken and egg problem here, but exploring this in a unit test doesn't indicate any problems. I want to understand what is going on here. I would have thought that since enums are static and final the MyEnum constructor would run when the JVM loads MyClass. However in my test it prints "getValue" before "MyEnum constructor".
MyClass {
private enum MyEnum {
VALUE;
MyEnum() {
System.out.println("MyEnum constructor");
MyClass clazz = new MyClass();
}
}
public MyEnum getValue() {
System.out.println("getValue");
return MyEnum.VALUE;
}
}
public class MyClassTest {
@Test
public void testStuff() {
MyClass clazz = new MyClass();
clazz.getValue();
}
}