Home  >  Article  >  Java  >  Detailed explanation of how to use private constructor in Java

Detailed explanation of how to use private constructor in Java

黄舟
黄舟Original
2017-09-14 10:48:521855browse

This article mainly introduces the meaning and keywords of Java private constructor, and also shows you how to use it through examples. Friends in need can refer to

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

Getting to the point, what is a private constructor? The so-called private constructor is a constructor declared with the private keyword, that is, a constructor declared with the private keyword. . The biggest difference from a general public constructor is that its access permission is private, so it can only be accessed by the class that contains 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 and no public constructors, no objects can be generated.

Since the constructor of the class is private, this class cannot be instantiated and cannot be inherited. Article 3 of "Effective Java": Use private constructors or enumerations to enhance Singleton properties. The so-called Singleton attribute refers to a class that is instantiated only once. Article 4: Enhance non-instantiability capabilities through private constructors. There are two ways to implement Singleton in Java:


public class Elvis {
 public static final Elvis INSTANCE = new Elvis();
 private Elvis() {}
}


public class Elvis {
 private static final Elvis INSTANCE = new Elvis();
 private Elvis() {}
 public static Elvis getInstance() { return INSTANCE;}
}

Method 1: The private constructor can only be called once. Instantiate the public static final field Elvis.INSTANCE. Once Elvis is instantiated, there will only be one Elvis instance (privileged clients can use the AccessibleObject.setAccessible method to call the private constructor through the reflection mechanism);

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

Why do we need a private constructor? How to use the method of this class if the class cannot be instantiated?

The existence of private constructors can prevent certain 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 You can define public static methods to achieve this, such as the use of private constructors in A.methon()


public class A {
 private A() {}
 public static void methon() {}    
}

java.lang.Math. You can see that the Math class is defined. It is final and uses a private constructor. Its methods are all static, so calling its methods only requires Math.sin(x):


public final class Math {
 private Math() {}
 public static final double E = 2.7182818284590452354;
 public static final double PI = 3.14159265358979323846;
 public static double sin(double a) {
  return StrictMath.sin(a); // default impl. delegates to StrictMath
 } 
 public static double cos(double a) {
  return StrictMath.cos(a); // default impl. delegates to StrictMath
 }
 public static double tan(double a) {
  return StrictMath.tan(a); // default impl. delegates to StrictMath
 }
 ......
 public static float scalb(float f, int scaleFactor) {
  return sun.misc.FpUtils.scalb(f, scaleFactor);
 }
}

The above is the detailed content of Detailed explanation of how to use private constructor in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn