Java private constructor usage example

As a special member of Java class, constructor can also set keywords to control its access rights. In most cases, we generally set the constructor as a public member, that is, public. By default, if no keyword is written, its access permission is also public. In this way, when we create a new class object, the constructor will be called automatically after the object is created, after other class members are set to the default initial value. Of course, if there are field initializers and initialization blocks, the constructor will be called after this. The main function of the constructor is to initialize the object before new returns the reference of the object.

To get back to business, what is a private constructor. The so-called private constructor is the constructor declared with the private keyword, that is, the constructor declared with the private keyword.. The biggest difference from the general public constructor is that its access permission is private, so it can only be accessed by the class containing it, and cannot be called outside the class, so it can prevent the generation of objects. Therefore, if a class has only one private constructor without any public constructor, it cannot generate any objects.

Because the constructor of a class is private, this class cannot be instantiated or inherited. Article 3 of effective Java: strengthen the singleton attribute with a private constructor or enumeration. The singleton attribute refers to a class that is instantiated only once. Article 4: strengthen the ability of non instantiation through private constructors. There are two ways to implement singleton in Java:

Method 1: the private constructor can only be called once to instantiate the public static final domain Elvis Instance: once Elvis is instantiated, only one Elvis instance will exist (the privileged client can call the private constructor through the reflection mechanism through the accessibleobject.setaccessible method);

Method 2: use the static method getInstance to return a reference to the same object and never create other evlis instances.

Why do you need a private constructor? If a class cannot be instantiated, how do you use the methods of this class?

The existence of private constructors can prevent some classes from being instantiated and subclassed. These classes are usually tool classes, such as Java Lang. math, etc. to access the methods of these classes, we can define public static methods to implement, such as a. METHON ()

java. Using the private constructor in lang. math, you can see that the math class is defined as final and uses the private constructor. Its methods are static, so only math. Is required to call its methods Sin (x):

I hope the content of the Java programming private constructor described in this article will be helpful to you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>