Access-level private of Constructor

Source: Internet
Author: User

Access Level of the constructor:

Public, protected, private, and default access level. Here, we will record the construction method modified by private.

Access Level of the private modifier constructor:

When the constructor is not declared as private, it means that only the method of the current class can be called. 1. Other constructor of the current class can be called using the this keyword. II. The member method of the current class can be called through the new statement.

The reason for declaring the constructor as private:

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 the details of its own implementation and does not allow othersProgramUse the new statement to create an instance of this class. This class provides other programs with a static method to obtain their own instances. This method is called a static factory method.

 

Question 1: Some people may have a question: "classes modified with abstract modifiers cannot be used to create instances. What is the difference between this and constructing access with private access permission words ?"

Question 2: The class modified by the final modifier cannot be inherited. What is the difference between this and constructing an access using the private access permission word?

 

Cause: the constructor using the private access restriction term limits the class, indicating that the class cannot be inherited or created.

Stract, which can be inherited and cannot be used to create instances.

final, which cannot be inherited and can be used to create instances.

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.