Can you make Private Constructor in Java?

Can you make Private Constructor in Java?


Declaring a private constructor is a very rare usage. First of all can we do it?

Yes, a constructor can be private and a private constructor can be overloaded also.

Following program illustrates.

In the above program, Test class created two private constructors – default and overloaded with int parameter. Program works nice. Observe the above screenshot.

Then, what is the use of declaring a constructor private?

Programmer can achieve two functionalities with a private constructor.

1. Composition (or has-a relationship) is not possible with default constructor. That is, you cannot create an object of the class in another class with default constructor.
2. The class with private constructor is not fit for inheritance. That is, the class cannot be extended.

The above two statements are proved programmatically hereunder.

1. Composition is not possible

In the following code, Test class includes a private default constructor.

The Demo class cannot create an object of Test in its main() method. Observe the screenshot about the compiler message. If the constructor is not private, the Demo can do it within no time as usual.

2. Not fit for inheritance

The class with private constructor cannot be extended. Observe the following code.

The above program raises compilation error. Why? We know a subclass constructor class super class constructor implicitly.

Demo d1 = new Demo();

The subclass Demo() constructor calls super class Test() constructor implicitly. But it cannot as Test() constructor has private access. For this reason, Test() cannot be extended by any other class,

Can you make class not to be extended by another without declaring it final?

Yes, simple, declare the constructor as private.

What other access modifiers a constructor cannot be?

"A constructor cannot be abstract, static, final, native, strictfp, or synchronized". Explained clearly in Java Constructor Properties

9 thoughts on “Can you make Private Constructor in Java?

  1. mohit

    hi sir ,
    as u have sad here that if we declare constructor as private then that class can’t have “has a relationship” .. i made this prog and it is running fine and it has “has a relationship” also ..please tell me where i can be wrong .

    class Test
    {
    private Test()
    {
    System.out.println(“I am default private constructor”);
    }
    public Test(int x)
    {
    this();
    System.out.println(“I am overloaded private constructor. ” +x);
    }

    }
    public class run {
    public static void main (String[] args){
    Test t = new Test(24);
    // t.Test(20);
    }
    }

  2. Abhishek kumar Gangwar

    I have a question, static modifier (keyword) can use instance variable, neither methods nor class. But a class declared as also
    static class action implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    JFrame frame2=new JFrame(“Frame2-Demo”);
    Label label=new Label(“You clicked Me.”);
    }
    }
    My Objection : static keyword can’t use with the class in your Site.why please Elaborate.
    on my Email: abhishekgangwar273241@gmail.com

  3. test2

    Merely wanna say that this is extremely helpful, Thanks for taking your time to write this. “We do not attract what we want, But what we are.” by James Allen.

Leave a Reply

Your email address will not be published. Required fields are marked *