Skip to main content

(Not So) Stupid Questions 15: How Can a Constructor Be Private?

November 28, 2006

Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...." If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.

Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to

This may be a stupid question, but ... "How can a constructor be private?"

First thoughts:

Constructors, like other methods, can have the access modifiers public, protected, and private, or not have an access modifier at all. If a method is private, then other classes can't call the method--it can only be called from within the class itself.

All of this is true of constructors, just like other methods. So it's legal to write:

public class MyClass {
    private MyClass() {

This will compile, but I don't see the point. The private modifier on the constructor prevents anything but an instance of this class from calling the constructor. But for there to be an instance at all, the constructor would have to be called somehow.

So I don't get it:

What's the point of having a private constructor?

width="1" height="1" border="0" alt=" " />
Related Topics >> Programming   |   


Overloaded constructors often

Overloaded constructors often call each other e.g.

public class MyTime {
public MyTime (int hour, int minutes) {
. . . .
public MyTime (String s) {
int hour = parsehour (s);
int minutes = parseminutes (s);
this(hours, minutes);

It generally makes sense for all constructors to be public but I suppose there might be circumstances where it makes sense for one or more to be private.

It makes sense when you want to implement a Singletone ...

It makes sense when you want to implement a Singletone design pattern, For example If you have an imput manager class that controls all the imput in the application, you don''t want to instanciate that class evrey time u need it so you just need to take its reference call its methods, and instanciate it only once at the startup point of the application. If you declare its constructor as private u make sure that no one instanciate it.