Skip to content

Santhosh Ponnam

Technology Blog

  • Home
  • About
  • Technology
    • Java
    • ORM
  • Privacy Policy
  • Toggle search form
  • Unique Random ‘N’ digit Number generator Java
  • JPA vs Hibernate – Specification Vs Implementation ORM
  • Windows Commands – kill port number Installations and Configurations
  • Log4j Vulnerability / Version Upgrade to 2.16.0 by Apache Team Java
  • Another Log4j Bug – DoS Java
  • hashCode and equals methods in java Java
  • Setting Environment Variables in Windows Java
  • Sonar Qube – Code Coverage and Code Quality Tool Installations and Configurations

Why to have a private constructor?

Posted on August 13, 2018September 1, 2021 By Santhosh Ponnam No Comments on Why to have a private constructor?

In Java, it is possible to have a private constructor. When and why should we use private constructor is explained in detail below. Defining a constructor with the private modifier says that only the native class (as in the class in which the private constructor is defined) is allowed to create an instance of the class, and no other caller is permitted to do so.

There are two possible reasons why one would want to use a private constructor
a. You don’t want any objects of your class to be created at all
b. You only want objects to be created internally –as in only created in your class.

Private constructors can be used in the singleton design pattern
A singleton is a design pattern that allows only one instance of your class to be created, and this can be accomplished by using a private constructor.

Private constructors can prevent creation of objects
The other possible reason for using a private constructor is to prevent object construction entirely. When would it make sense to do something like that? Of course, when creating an object doesn’t make sense – and this occurs when the class only contains static members. And when a class contains only static members, those members can be accessed using only the class name – no instance of the class needs to be created.

Java always provides a default, no-argument, public constructor if no programmer-defined constructor exists. Creating a private no-argument constructor essentially prevents the usage of that default constructor, thereby preventing a caller from creating an instance of the class. Note that the private constructor may even be empty.

Java Tags:constructor, private

Post navigation

Previous Post: hashCode and equals methods in java
Next Post: Windows Commands – kill port number

Related Posts

  • Another Log4j Bug – DoS Java
  • Setting Environment Variables in Windows Java
  • Log4J (RCE) Vulnerability Java
  • Unique Random ‘N’ digit Number generator Java
  • Fixing Log4j Vulnerability Java
  • Log4j Vulnerability / Version Upgrade to 2.16.0 by Apache Team Java

Leave a Reply Cancel reply

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

Recent Posts

  • Another Log4j Bug – DoS
  • Log4j Vulnerability / Version Upgrade to 2.16.0 by Apache Team
  • Fixing Log4j Vulnerability
  • Log4J (RCE) Vulnerability
  • JPA vs Hibernate – Specification Vs Implementation

Categories

  • Installations and Configurations
  • Java
  • ORM
  • Spring Boot
  • Spring Boot HikariCP Connection Pooling

Archives

  • December 2021
  • August 2021
  • November 2020
  • March 2020
  • August 2018
  • November 2016
  • August 2016

Recent Posts

  • Another Log4j Bug – DoS
  • Log4j Vulnerability / Version Upgrade to 2.16.0 by Apache Team
  • Fixing Log4j Vulnerability
  • Log4J (RCE) Vulnerability
  • JPA vs Hibernate – Specification Vs Implementation

Categories

  • Installations and Configurations
  • Java
  • ORM
  • Spring Boot
  • Spring Boot HikariCP Connection Pooling

Archives

  • December 2021
  • August 2021
  • November 2020
  • March 2020
  • August 2018
  • November 2016
  • August 2016




Recent Posts

  • Another Log4j Bug – DoS
  • Log4j Vulnerability / Version Upgrade to 2.16.0 by Apache Team
  • Fixing Log4j Vulnerability
  • Log4J (RCE) Vulnerability
  • JPA vs Hibernate – Specification Vs Implementation

Categories

  • Installations and Configurations
  • Java
  • ORM
  • Spring Boot
  • Spring Boot HikariCP Connection Pooling
  • Log4J (RCE) Vulnerability Java
  • JPA vs Hibernate – Specification Vs Implementation ORM
  • Another Log4j Bug – DoS Java
  • Unique Random ‘N’ digit Number generator Java
  • Sonar Qube – Code Coverage and Code Quality Tool Installations and Configurations
  • Setting Environment Variables in Windows Java
  • hashCode and equals methods in java Java
  • Log4j Vulnerability / Version Upgrade to 2.16.0 by Apache Team Java

Copyright © 2023 Santhosh Ponnam.

Powered by PressBook News WordPress theme