Learn Java by Example: Java Program to implement private constructors

Hits: 6

Java Program to implement private constructors

In this example, we will learn to implement private constructors in Java.

 


Example 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();
  }
}

Output

This is a private constructor.

In the above example, we have created a private constructor of the Test class. Hence, we cannot create an object of the Test class outside of the class.

This is why we have created a public static method named instanceMethod() inside the class that is used to create an object of the Test class. And from the Main class, we call the method using the class name.


Example 2: Java Singleton design using a private constructor

The Java Singleton design pattern ensures that there should be only one instance of a class. To achieve this we use the 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();
  }
}

Output

Inside Private Constructor
Singleton Pattern is achieved

In the above example, we have created a class named Languages. The class contains,

  • language – class type private variable
  • Language() – private constructor
  • getInstance() – public static class type method
  • display() – public method

 

Since the constructor is private, we cannot create objects of Language from the outer class. Hence, we have created an object of the class inside the getInstance() method.

However, we have set the condition in such a way that only one object is created. And, the method returns the object.

Notice the line,

db1 = Language.getInstance();

Here,

  • db1 is a variable of Language type
  • Language.getInstance() – calls the method getInstance()

 

Since, getInstance() returns the object of the Language class, the db1 variable is assigned with the returned object.

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

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.