Welcome to the World of Online Learning:
Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Write a Java Program to implement private constructors
PROGRAM: Java Program to implement private constructors
/*Java Program to implement private constructors*/
1: 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 Test class
Test obj = new Test();
}
}
class Main {
public static void main(String[] args) {
// call the instanceMethod()
Test.instanceMethod();
}
}
2: Java Singleton design using a private constructor
class Language {
// create a public static variable of class type
private static Language language;
// private constructor
private Language() {
System.out.println(“Inside Private Constructor”);
}
// public static method
public static Language getInstance() {
// create object if it’s not already created
if(language == null) {
language = new Language();
}
// returns the singleton object
return language;
}
public void display() {
System.out.println(“Singleton Pattern is achieved”);
}
}
class Main {
public static void main(String[] args) {
Language db1;
// call the getInstance method
db1= Language.getInstance();
db1.display();
}
}