Write a program in Java to demonstrate the use of private constructor and also write a method which will count the number of instances created using default constructor only.

 

AIM: Write a program in Java to demonstrate the use of private constructor and also write a method which will count the number of instances created using default constructor only.

 

CODE:

class Singleton{

   static int count=0;

   //private instance Object of same class

  private static Singleton single_instance = null;

   //private constructor

   private Singleton() {

               count++; 

            }

   // Method that creates and returns same class that is Singleton Object.

   static public Singleton createInstance(){

                        if(single_instance==null){

                                    single_instance = new Singleton();

                        }

   return single_instance;   

   } 

   void display(){

                        System.out.println("Count: "+count);

   }

} 

class SingletonMain{    

                        public static void main(String args[]) { 

                                    Singleton s1 = Singleton.createInstance();

                                    Singleton s2 = Singleton.createInstance();

                                    s1.display();

                                    s2.display();   

                        }  

 }

OUTPUT:




Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program in Java to perform I/O operations on a Text file.