How does an enum singleton function?
enums, instance, java, singleton
Solution
The Java compiler takes care of creating enum fields as static instances of a Java class in bytecode. Great blog post on it (not my blog) with bytecode here: http://boyns.blogspot.com/2008/03/java-15-explained-enum.html
Problem
Previously instead of using enums, I would do something like: ``` public static ExampleClass instance; public ExampleClass(){ instance=this; } public static ExampleClass getInstance(){ return instance; } ``` Then someone told me about a enum singleton: ``` public enum Example{ INSTANCE; public static Example getInstance(){ return Example.INSTANCE; } ``` In the first example I had to instantiate the object in order to create the instance. With an enum, I do not need to do that.. at least it appears. Can someone explain the reason behind this?