简体   繁体   中英

How to use an instance of a class that has a private constructor?

If we have class A & B, and class A's constructor is private, and we want to use an instance of A in B, how to do that ? I see an answer that says "provide a static method or variable that allows access to an instance created from within the class " but I didn't understand that.

The code pattern you seek is called the Factory Method .

The class provides a static method that returns an instance of its own class. Private constructors are visible to all methods (including static ones) of the class, so the static method can invoke the private constructor on the caller's behalf.

Here's an example of this pattern in action:

public class A {
    private A() {
    }

    public static A create() {
        return new A();
    }
}

This is often employed in conjunction with the Singleton Pattern , which would change the above example to this:

public class A {
    private static A INSTANCE = new A();

    private A() {
    }

    public static A getInstance() {
        return INSTANCE;
    }
}

A needs to have a public method that provides an instance of the class A, eg:

class A {
  /*Constructors and other methods omitted*/
  public static A getInstance() {
    return new A();
  }
}

Alternatively, if B is an inner class of A (or vice-versa), then B can directly reference the constructor eg:

public class A {
    private A() {}

    public static class B {
        private A instanceOfA = new A();

        public B() {}
    }
}

A class that only has private constructors is designed so that other classes cannot instantiate it directly. Presumably there is a sound reason for this. The class may provide a factory method for instantiating the class ... or getting an existing instance of the class.

If you need to change the design, the best way is to modify the class; eg by making a constructor visible, or by adding a factory method. If you can't do that, I think it is possible to use reflection to break the visibility rules and create an instance using a private constructor. However, I'd only do this as a last resort ... and not before carefully analysing the consequences for the overall application.

Private constructors are intended to make a class not to have any instance. But the content can be accessed from child class using super() . Implementation is like this:

public class ClassA {
private int val;
private ClassA(int val)
{

  this.val = val; 

}

public int getVal() { return val;
}

}

public class ClassB extends ClassA {

public ClassB(int val) { super(val); } }

...

ClassB b = new ClassB(4);

System.out.println("value of b: " + b.getVal());

As an example see class Calendar . To get an instance you must not call its constructor but use a static method:

Calendar rightNow = Calendar.getInstance();

source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM