Java Review-singleton Class

Source: Internet
Author: User

Most of the time, the class's constructor is defined as public access, allowing any class to freely create objects of that class, but at other times it can degrade the performance of the system, such as the cost of reclaiming objects, and in order to avoid the freedom of other classes to create the class, we can set the constructor of the class to a private property. Once the constructor of the class is defined as a private property, a public method must be provided, and a static adornment, because the object is not present when the method is called, so it is not an object to call the method, only a class, in addition to the object that has already been created must be cached. Otherwise, the class cannot know whether an object was created before, and there is no guarantee that an object is created, for which the class needs to use a member object to hold the object that was created, because the member variable needs to be accessed by the variable above.

Package Myproject;
public class Singleton {
private static Singleton Instanse;
Private Singleton () {}
public static singleton getinstance () {
if (instanse==null) {
Instanse=new Singleton ();
}
return instanse;
}

}

Package Myproject;
public class Mysingleton {
public static void Main (string[] args) {
Singleton s1=singleton.getinstance ();
Singleton s2=singleton.getinstance ();
}
}

Singleton S3=new Singleton ();//Such declaration method is wrong, cannot find the way of declaring.

Java Review-singleton Class

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.