enum properties & side effects
enums, java
Solution
Enums are Singletons and they are instanciated upon loading of the class - so the two "hello"s come from instanciating `MyEnum.ONE` and `MyEnum.TWO` (just try printing `value` as well).
This is also the reason why the constuctor must not be public: the Enum guarantees there will ever only be one instance of each value - which it can't if someone else could fiddle with the constructor.
Problem
I have a question regarding enum (it might be a simple one but ....). This is my program: ``` public class Hello { public enum MyEnum { ONE(1), TWO(2); private int value; private MyEnum(int value) { System.out.println("hello"); this.value = value; } public int getValue() { return value; } } public static void main(String[] args) { MyEnum e = MyEnum.ONE; } } ``` and my question is: Why the output is ``` hello hello ``` and not `hello` ? How the code is "going" twice to the constructor ? When is the first time and when is the second ? And why the enum constructor can not be public ? Is it the reason why it print twice and not one time only ?