+1 vote
in JAVA by
What is a singleton class?

1 Answer

0 votes
by
Singleton class is the class which can not be instantiated more than once. To make a class singleton, we either make its constructor private or use the static getInstance method. Consider the following example.

class Singleton{  

    private static Singleton single_instance = null;  

    int i;  

     private Singleton ()  

     {  

         i=90;  

     }  

     public static Singleton getInstance()  

     {  

         if(single_instance == null)  

         {  

             single_instance = new Singleton();  

         }  

         return single_instance;  

     }  

}  

public class Main   

{  

    public static void main (String args[])  

    {  

        Singleton first = Singleton.getInstance();  

        System.out.println("First instance integer value:"+first.i);  

        first.i=first.i+90;  

        Singleton second = Singleton.getInstance();  

        System.out.println("Second instance integer value:"+second.i);  

    }  

}

Related questions

0 votes
asked Apr 9, 2021 in JAVA by Robindeniel
0 votes
asked Jan 24, 2021 in JAVA by rajeshsharma
...