Private Constructors and Singleton Classes in Java Last Updated : 24 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the private constructor is a special type of constructor that cannot be accessed from outside the class. This is used in conjunction with the singleton design pattern to control the instantiation. Private ConstructorThe private constructor is used to resist other classes to create new instances of the class. It can be only accessed within the class. These are used to create singleton design patterns such as in Math class. private constructors are used in different scenarios such as Internal Constructor chaining and Singleton class design patterns.Singleton ClassesThe Singleton pattern is a design pattern that is used to ensure that the class has only one instance which can be possible using the private constructor. In a singleton design pattern, there is a global point access which is used to access the classes and their properties. The private constructor is the main characteristic of singleton design in Java.The constructor of the singleton class would be private so there must be another way to get the instance of that class. This problem is resolved using a class member instance and a factory method to return the class member.characteristicExample: Implementing the singleton design pattern using the private constructor. Java // Java program to demonstrate implementation of Singleton // pattern using private constructors. import java.io.*; class MySingleton { static MySingleton instance = null; public int x = 10; // private constructor can't be accessed outside the class private MySingleton() { } // Factory method to provide the users with instances static public MySingleton getInstance() { if (instance == null) instance = new MySingleton(); return instance; } } // Main Class class Geeks { public static void main(String args[]) { MySingleton a = MySingleton.getInstance(); MySingleton b = MySingleton.getInstance(); a.x = a.x + 10; System.out.println("Value of a.x = " + a.x); System.out.println("Value of b.x = " + b.x); } } OutputValue of a.x = 20 Value of b.x = 20 Explanation: The above program makes sure that only one instance of the MySingleton class is created with the help of a private constructor. Here we changed the value of a.x, value of b.x also got updated because both ‘a’ and ‘b’ refer to the same object, i.e., they are objects of a singleton class. Comment More infoAdvertise with us Next Article Copy Constructor in Java K kartik Improve Article Tags : Java Practice Tags : Java Similar Reads Java Constructors In Java, constructors play an important role in object creation. A constructor is a special block of code that is called when an object is created. Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes. This process happens automaticall 10 min read Default constructor in Java Like C++, Java automatically creates default constructor if there is no default or parameterized constructor written by user, and (like C++) the default constructor automatically calls parent default constructor. But unlike C++, default constructor in Java initializes member data variable to default 1 min read Private Constructors and Singleton Classes in Java In Java, the private constructor is a special type of constructor that cannot be accessed from outside the class. This is used in conjunction with the singleton design pattern to control the instantiation. Private ConstructorThe private constructor is used to resist other classes to create new insta 2 min read Copy Constructor in Java Like C++, Java also supports a copy constructor. But, unlike C++, Java doesn't create a default copy constructor if you don't write your own. A prerequisite prior to learning copy constructors is to learn about constructors in java to deeper roots. Below is an example Java program that shows a simpl 4 min read Constructor Chaining In Java with Examples Constructor chaining is the process of calling one constructor from another constructor with respect to current object. One of the main use of constructor chaining is to avoid duplicate codes while having multiple constructor (by means of constructor overloading) and make code more readable. Prerequ 5 min read Why Constructors are not inherited in Java? Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super clas 2 min read Constructor Overloading in Java Java supports Constructor Overloading in addition to overloading methods. In Java, overloaded constructor is called based on the parameters specified when a new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be do 5 min read Static Blocks in Java In simpler language whenever we use a static keyword and associate it to a block then that block is referred to as a static block. Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the 4 min read The Initializer Block in Java In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initial 2 min read Order of Execution of Initialization Blocks and Constructors in Java In Java, there are various techniques, which we can use to initialize and perform operations on objects such as methods, constructors, and initialization blocks. These tools are used to ensure that the program works as expected. Instance Initialization Blocks (IIB) are used to initialize instance va 4 min read Like