Best single template with Java 5

Since Java 5 says the best way to create a singleton is with a singleton enum type.

Example:

public enum SuperSingleton implements Zooma{ INSTANCE; /** */ public void fightTheBattle(){ System.out.println("I am fighting the battle!!!"); } @Override public void runningWild() { //This is method implemented from the Zooma interface. } } 

According to Joshua Bloch, a singleton singleton singleton:

  • more concise
  • provides serialization machines for free
  • and provides distinct from multiple instances.

I can see how much shorter it is and how it provides a monstrous character against a multiple instance , but how does it provide free serialization?

Is this something singleton gets by listing?

+3
java enums singleton java-5
source share
3 answers

Yes, the enumerations are all extended from the Enum class , which implements Serializable .

+3
source share

I'm not 100% sure, but I think that if you deserialize a serialized singleton more than ever, you can get more than one instance. An enum instance will always remain a single.

So, you get "more serialization" than what you get from implementing serialization.

+1
source share

All Articles