Java Singleton mode &static member variable differences

Source: Internet
Author: User

When there are many variables that need to be shared, it takes too long to use a static variable for the entire life cycle of the class. The object is only present in the entire life cycle of the object.

// a hungry man type class Single// class once loaded, the object already exists.  {privatestaticnewprivatepublic  static single getinstance () {return  s;}}
//Lazy TypeclassSingle2//class is loaded, there are no objects, and only objects are created when the GetInstance method is called. //deferred loading form. There is a security risk in the concurrency process. {Private StaticSingle2 s =NULL; PrivateSingle2 () {} Public StaticSingle2 getinstance () {if(s==NULL) s=NewSingle2 ();returns;}}

Java Singleton mode &static member variable differences

Related Article

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.