1

For example a class:

//class1
class A {
    private A() {  } // why would I make it private?
    public A(int) {  } //why isn't it implicitly public?
}
//class2
class B {

    public static void main(String[] args) {
        //A a = new A();
    }
}

A constructor instantiates a class so why it has the access modifier? Is there a case when we have to declare a constructor private?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Ahmad
  • 21
  • 8

3 Answers3

6

A constructor instantiates a class so why it has the access modifier?

The modifier can be used so you control where the object can be constructed.

Is there a case when we have to declare a constructor private?

Say you have a factory method like

class A {
    private A(int n)  { }

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

or you have a shared constructor which should be called directly.

class B {
    public B(int n) {
        this(n, "");
    }
    public B(String str) {
        this(0, str);
    }
    private B(int n, String str) { }
}

or you have a Singleton

final class Singleton {
    Singleton INSTANCE = new Singleton();
    private Singleton() { }
}

however I prefer to use an enum which has a private constructor.

enum Singleton {
    INSTANCE;
}

or you have a Utility class

final class Utils {
    private Utils() { /* don't create one */ }

    public static int method(int n) { }
}

however I prefer to use an enum in this case

enum Utils {
    /* no instances */;

    public static int method(int n) { }
}

Note: if you use a private constructor on a final class you can still create instances using nested classes, or reflection. If you use an enum you can't create an instance as easily/accidentally.

Warning: You can create instances of an enum using Unsafe


Note in enum the constructor has to be private

class BuySell {
    BUY(+1), SELL(-1);

    private BuySell(int dir) { }
}

You don't have to make it private explicitly as this is the default.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    @TheLostMind I call that a utility class, as mentioned at the end. I prefer to use an `enum` which has a private constructor. – Peter Lawrey Apr 10 '16 at 07:19
1

The private modifier when applied to a constructor works in much the same way as when applied to a normal method or even an instance variable. Defining a constructor with the private modifier says that only the native class (as in the class in which the private constructor is defined) is allowed to create an instance of the class, and no other caller is permitted to do so. There are two possible reasons why one would want to use a private constructor – the first is that you don’t want any objects of your class to be created at all, and the second is that you only want objects to be created internally – as in only created in your class.

Uses of private construtor:-
1) Private constructors can be used in the singleton design pattern
2) Private constructors can prevent creation of objects

This might also help Can a constructor in Java be private? the use cases of private constructor

Community
  • 1
  • 1
Naruto
  • 4,221
  • 1
  • 21
  • 32
0

Constructor are not responsible for Creating a object of a Class, these constructor are only responsible for initialize the member variables only.

There are various Reason behind this. One of the most popular reason behind this is design-Pattern.

//class1
class A {
    private A() {  } // why would I make it private?

}

why make private Constructor ?

If you want to make a Class singleton then your constructor must be private. then only it is possible to make a Class a Singleton.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52