Can a Constructor in Java Be Private

Do constructors always have to be public?

No, Constructors can be public, private, protected or default(no access modifier at all).

Making something private doesn't mean nobody can access it. It just means that nobody outside the class can access it. So private constructor is useful too.

One of the use of private constructor is to serve singleton classes. A singleton class is one which limits the number of objects creation to one. Using private constructor we can ensure that no more than one object can be created at a time.

Example -

public class Database {

private static Database singleObject;
private int record;
private String name;

private Database(String n) {
name = n;
record = 0;
}

public static synchronized Database getInstance(String n) {
if (singleObject == null) {
singleObject = new Database(n);
}

return singleObject;
}

public void doSomething() {
System.out.println("Hello StackOverflow.");
}

public String getName() {
return name;
}
}

More information about access modifiers.

Use of a private constructor

It is a private constructor. This means that outside classes cannot create new instances using the default constructor.

A little more info

All Objects in Java have a default constructor:

public MyObject() {}

That is how you can have this class:

public class MyObject{}

and still be able to call:

MyObject mObj = new MyObject();

Private Constructors

Sometimes a developer may not want this default constructor to be visible. Adding any other constructor will nullify this constructor. This can either be a declared constructor with empty parameters (with any of the visibility modifiers) or it can be a different constructor all together.

In the case above, it is likely that one of the following models is followed:

  1. The Settings object is instantiated within the Settings class, and is where all the code is run (a common model for Java - where such a class would also contain a static main(String[] args) method).

  2. The Settings object has other, public constructors.

  3. The Settings object is a Singleton, whereby one static instance of the Settings Object is provided to Objects through an accessor method. For example:


public class MyObject {
private static MyObject instance;
private MyObject(){}//overrides the default constructor
public static MyObject sharedMyObject() {
if (instance == null)
instance = new MyObject();//calls the private constructor
return instance;
}
}

Why do we need a private constructor at all?

We can't instantiate more than one object at a time via private constructors.

No, we can. A private constructor only avoids instance creation outside the class. Therefore, you are responsible for deciding in which cases a new object should be created.

And as expected both the strings are being printed. Did I miss something?

You are creating a new instance every time the getInstance method is called. But your commented code contains a standard lazy initialization implementation of the singleton pattern. Uncomment these parts to see the difference.

Other singleton implementations you can look over here.

Java private constructor with parameters

Yes, if you are going to use that constructor in some method of your class itself and expose the method to other class like we do in the singleton pattern. One simple example of that would be like below :

public class MySingleTon {    
private static MySingleTon myObj;
private String creator;
private MySingleTon(String creator){
this.creator = creator;
}
public static MySingleTon getInstance(String creator){
if(myObj == null){
myObj = new MySingleTon(creator);
}
return myObj;
}
public static void main(String a[]){
MySingleTon st = MySingleTon.getInstance("DCR");
}
}


Related Topics



Leave a reply



Submit