Singleton class (single-State class, only one instance exists)

Source: Internet
Author: User
Document directory
  • Not long ago, I met an interview question, which gave me only one class object for the new instance. I didn't turn around for a moment. I came back to Google to check and realize that this could be answered in a disguised form in single-State mode.
Not long ago, I met an interview question, which gave me only one class object for the new instance. I didn't turn around for a moment. I came back to Google to check and realize that this could be answered in a disguised form in single-State mode.

Keyword: Singleton

The Singleton mode ensures that only one instance of a class exists in a Java application.
The Singleton mode generally has several forms:
The first form: defines a class. Its constructor is private. It has a static private class variable. When the class is initialized, use a public getinstance method to obtain its reference, and then call the method. Java code
  1. Public


     
    Class


    Singleton {

  2. Private


    Singleton (){}

  3. // Define your own instance internally


  4. // Note that this is private for internal calls only


  5. Private


     
    Static


    Singleton instance =
    New


    Singleton ();

  6. // Here is a static method for external access to this class, which can be accessed directly.


  7. Public


     
    Static


    Singleton getinstance (){

  8. Return


    Instance;
  9. }
  10. }
Public class Singleton {private Singleton () {}// define your own instance internally // note that this is private for internal calls only Private Static Singleton instance = new Singleton (); // here is a static method for external access to this class. You can directly access public static Singleton getinstance () {return instance ;}}

Form 2: Java code

  1. Public


     
    Class


    Singleton {

  2. Private


     
    Static


    Singleton instance =
    Null


    ;

  3. Public


     
    Static


     
    Synchronized


    Singleton getinstance (){

  4. // This method is better than above. You don't need to generate objects every time. It's just the first time.


  5. // Generate instances during use, improving efficiency!


  6. If


    (Instance =
    Null


    )
  7. Instance =
    New


    Singleton ();

  8. Return


    Instance;
  9. }
  10. }
Public class Singleton {Private Static Singleton instance = NULL; public static synchronized Singleton getinstance () {// This method is better than above, so you do not need to generate objects every time, it is only the first time that an instance is generated/used, improving the efficiency! If (instance = NULL) instance = new Singleton (); Return instance ;}}

Other forms:
Defines a class. Its constructor is private and all methods are static.
It is generally considered that the first form is more secure.

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.