English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input and output (I/O)

Java Reader/Writer

Java other topics

Java program implementation of private constructor

Comprehensive Java Examples

In this example, we will learn how to implement a private constructor using Java.

To understand this example, you should understand the followingJava programmingTopic:

Example1: Java program to create a private constructor

class Test {
  //Create private constructor
  private Test() {
    System.out.println("This is a private constructor.");
  }
  //Create a public static method
  public static void instanceMethod() {
    //Create an instance of the Test class
    Test obj = new Test();
  }
}
class Main {
  public static void main(String[] args) {
    //Call instanceMethod()
    Test.instanceMethod();
  }
}

Output result

This is a private constructor.

In the above example, we created the private constructor of the Test class. Therefore, we cannot create an object of this class outside the Test class.

This is why we created a public static method named instanceMethod() within the class, which is used to create an object of the Test class. In the Main class, we call this method using the class name.

Example2: Java singleton design pattern using private constructor

The Java singleton design pattern ensures that a class has only one instance. To do this, we use a private constructor.

class Language {
  //Create a public static variable of class type
  private static Language language;
  //Private constructor
  private Language() {
    System.out.println("Internal private constructor");
  }
  //Public static method
  public static Language getInstance() {
     //Create object (if not already created)
     if(language == null) {
        language = new Language();
     }
      //Return the singleton object
      return language;
  }
  public void display() {
      System.out.println("Singleton pattern implemented");
  }
}
class Main {
  public static void main(String[] args) {
     Language db1;
     //Call the getInstance method
     db1= Language.getInstance();
     db1.display();
  }
}

Output result

Internal private constructor
Singleton pattern implemented

In the above example, we created a class named Languages. This class includes:

  • language - Class type private variable

  • Language() - Private constructor

  • getInstance() - Public static class method

  • display() - Public method

Since the constructor is private, we cannot create a Language object from the external class. Therefore, we create an object of this class within the getInstance() method.

However, we set the condition to create only one object. And, this method returns an object.

Note this line,

db1 = Language.getInstance();

Here,

  • db1 is a variable of the Language type

  • Language.getInstance() - Call the method getInstance()

Since getInstance() returns an object of the Language class, therefore for db1The variable assigned the returned object.

Finally, we called the display() method using an object.

Comprehensive Java Examples