19. When we should use private constructor?

What is private contructor?

It allows to restrict instantiation of class.
That means, we can prevent create an instance of a class any place other than class itsself.

Example:

public class Singleton {
// Step 1: Create a static instance of the class (private static variable)
private static Singleton instance;

// Step 2: Create a private constructor to prevent the creation of new objects from outside the class
private Singleton() {
// Private constructor
}

// Step 3: Provide a public static method to get the unique instance
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

// Example method
public void showMessage() {
System.out.println(“Hello World from Singleton!”);
}
}

public class Main {
public static void main(String[] args) {
// Get the unique instance of the Singleton class
Singleton singleInstance = Singleton.getInstance();
}

When we should use private constructor?

There are some scenarios that we often use private constructor

+ Singleton Pattern

Example:

public class Singleton {
// Step 1: Create a static instance of the class (private static variable)
private static Singleton instance;

// Step 2: Create a private constructor to prevent the creation of new objects from outside the class
private Singleton() {
// Private constructor
System.out.println(“Singleton instance created.”);
}

// Step 3: Provide a public static method to get the unique instance
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

// Example method
public void showMessage() {
System.out.println(“Hello World from Singleton!”);
}

public static void main(String[] args) {
// Attempting to create two instances of Singleton class
Singleton singleInstance1 = Singleton.getInstance();
Singleton singleInstance2 = Singleton.getInstance();

// Both instances should be the same
System.out.println(“singleInstance1 hashcode: ” + singleInstance1.hashCode());
System.out.println(“singleInstance2 hashcode: ” + singleInstance2.hashCode());

// Calling method on Singleton instance
singleInstance1.showMessage();
}
}

+ Delegate Constructor : allow us to pass parameter through several different constructor but the constructor is private and it only can be use in class itself

+ Create  Utility class : it’s a class contain a collection of static method and we can’t create an instance with this class.

+ Builder Pattern : It allow us to create an instance of complex object step by step rather than having public constructor to create instance

  • Advantage of builder pattern :

+ Easy to read and understand

+ Prevent missing information with class has constructor with many parameters

+ Check constraint of field or validate information of the class

+ Create immutable class : We don’t have any setter method in the class, we can only create instances of a class through static class builder inside that class.

Exampale Builder Pattern:

// Build method to create the Person object
public Person build()
{
return new Person(this);
}
}
public static void main(String[] args)
{
// Creating a Person object using the Builder
Person person = new Person.PersonBuilder(“John”, “Doe”)
.age(30) .address(“123 Main St”)
.phoneNumber(“123-456-7890”)
.build();

Leave a Reply

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