Why Enum Singleton are better in Java Singleton using Enum in Java This is the way we generally declare...

Please Visit: http://ift.tt/1ajReyV



Why Enum Singleton are better in Java

Singleton using Enum in Java

This is the way we generally declare Enum Singleton , it may contain instace variable and instance method but for sake of simplicity I haven’t used any, just beware that if you are using any instance method than you need to ensure thread-safety of that method if at all it affect the state of object. By default creation of Enum instance is thread safe but any other method on Enum is programmers responsibility.

/**

* Singleton pattern example using Java Enumj

*/

public enum EasySingleton{

INSTANCE;

}

You can acess it by EasySingleton.INSTANCE, much easier than calling getInstance() method on Singleton.

http://ift.tt/1lBFHUQ

Done






from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/1GEOe2o

via LifeLong Community

No comments:

Post a Comment