Summary: some classes using private constructor and private constructor

Source: Internet
Author: User

Summary: some classes using private constructor and private constructor

The first is the singleton mode. Although it is divided into the lazy mode and the drunk mode, there is only one such object in jvm! This is called a singleton (refer to the design mode for details)

Second, the tool class. The tool class is private, because the behavior and attributes in the tool class are static, so jdk wants us not to instantiate it and only need to access it through the class name. If private is used, it cannot be inherited,

Differences between private constructor and final modifier classes:

Private, which cannot be inherited or created.

Final. It cannot be inherited and can be used to create instances.

The reason for declaring the constructor as private (excerpt ):

1. This class only contains static methods called by other classes, and there is no instance method. This means that when a class wants to call a method in this class, it does not need to create an instance of this class, that is, it does not touch the constructor of this class.

2. Do not inherit this class.

3. This class needs to encapsulate its own implementation details. Other programs are not allowed to create instances of this class through the new statement. This class provides other programs with a static method to obtain their own instances. This method is called a static factory method.

If there are other classes in JVM that use the private constructor, please let me know!

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.