Eslam Hamdy
Eslam Hamdy

Reputation: 7386

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.

Upvotes: 3

Views: 3585

Answers (6)

alaster
alaster

Reputation: 4171

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

Upvotes: 0

Stephen C
Stephen C

Reputation: 718758

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; e.g. 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.

Upvotes: 1

Chaithanya
Chaithanya

Reputation: 608

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());

Upvotes: 0

Adam Londero
Adam Londero

Reputation: 79

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() {}
    }
}

Upvotes: 1

Bohemian
Bohemian

Reputation: 424993

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;
    }
}

Upvotes: 5

Keppil
Keppil

Reputation: 46209

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();
  }
}

Upvotes: 3

Related Questions