Java single-instance enum implementation mode

Source: Internet
Author: User

The traditional two private one public (private construction method, private static instance (lazy instantiation/Direct instantiation), public static Fetch method) involves thread safety issues (even if multiple check locks can also be broken by reflection of a single case),

The most secure way to implement a singleton now is through an internal static Enum method, because the JVM guarantees that the enum cannot be reflected and that the constructor method executes only once.

The implementation method is as follows:

/*** Use the enumeration's singleton mode * *@authorYzl *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/ Public classenumsingleton{PrivateEnumsingleton () {} Public StaticEnumsingleton getinstance () {returnSingleton.INSTANCE.getInstance (); }        Private Static enumsingleton{INSTANCE; PrivateEnumsingleton Singleton; //the JVM will ensure that this method is absolutely called only once        PrivateSingleton () {Singleton=NewEnumsingleton (); }         PublicEnumsingleton getinstance () {returnSingleton; }    }}

Test method:

 Public Static void Main (string[] args) {    = enumsingleton.getinstance ();     = enumsingleton.getinstance ();     // output Result: Obj1==obj2?true    System.out.println ("Obj1==obj2" + (obj1==obj2));}

To extend the application, observe the following example

 Public class test{  // initialize something  static{       }}

This is a common way of initializing static resources within a class (which is actually another manifestation of the singleton-only once), but writing the code under the static block will look very elegant and can be initialized using the enum singleton pattern above.

See Example:

Importjava.util.ArrayList;Importjava.util.List;/*** Initialization of the elegant implementation * can be called in the static, * can also be called in the normal method, are guaranteed to initialize only once * * Of course, the code of the enum block directly into the private static method of the Staticinittest class is also possible * /c5>@authorYzl *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/ Public classStaticinittest {Private StaticList<integer> dataList =NULL; Static{dataList=Singleton.INSTANCE.init (); }        /*** * Single-case mode to populate data * *@authorYzl *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/    Private Static enumSingleton {INSTANCE; PrivateList<integer>list; PrivateSingleton () {filldata (); }        /*** * Initialize data * *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/        Private voidFilldata () {list=NewArraylist<integer> (5);  for(inti = 1; i<6; i++) {list.add (i); }        }        /*** * Initialize the portal * *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/         PublicList<integer>init () {returnlist; }    }}

Java single-instance enum implementation mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.