10 Singleton Pattern Interview Questions and Answers in java

Singleton pattern is one of the most widely asked pattern in interviews. So lets see some of the interview questions on Singleton Design Pattern in java.

10 Singleton Pattern in java Interview Questions and Answers





1. What is singleton class or singleton design pattern? Have you used any of these?


This might be the first question you will face while dealing with singleton or singleton design pattern.
A singleton is a class of which there can be only one instance in your application. You can get the instance using getInstance method and this instance can be shared throughout your application.
If you have really used singleton then tell you have used and explain how you have used it. If you have not used it, you can give examples of SingleTon used in JDK such as in java.lang.Runtime getRuntime() method and in java.awt.Desktop getDesktop() method.
To know more about Singleton design pattern click here.


2. Write a code for getInstance() method used in Singleton


This is a very tricky question, it is actually a door to more deeper questions about to come on Singleton. So unless you know about Double Locking mechanism, don’t write code using double locking mechanism, instead write simple mechanism of creating the getInstance() as shown below

 
class SingletonTest 
 {  
// 1. Make all constructors private - This is the main thing to remember :)  
private SingletonTest() {} 
 // 2. Second step is to declare a private and static variable which will hold our single instance of a class 
 private static Singleton instance = null;  
// 3. Now we will write public static getInstace() method 
 public static Singleton getInstance() {  
// 4. Simple initialization - lazy initialization 
       if(instance == null) {  
            instance = new Singleton(); 
       }  
return instance;  
}  
}  

3. Could you give some of the examples which follows Singleton Pattern in Standard Java Development Kit ?


This is also one of the important Singleton Pattern Interview Questions asked, there are many classes in standard Java Development Kit which follows Singleton Pattern such as java.lang.Runtime classs where we have the getRuntime() method, Java.awt.Desktop which has the getDesktop() method etc.

4.How we can create a thread safe Singleton in java? or in other words explain singleton and mulithreading.



This is a very important Singleton Pattern Interview Question because of two reasons, one is it shows what you know about the initialization ways in Singleton Pattern and as well as multithreading knowledge. The detail answer can be found at Singleton and multithreading in Java post.

5.Explain double checked locking in Singleton Pattern


In case of double checked locking before doing synchronization we are checking uniqueInstance as null or not, and in synchronization block also we are checking uniqueInstance is null or not before creating instance. And the most important thing is we declare uniqueInstance variable as volatile keyword. More information and example for double checked locking can be found at Singleton and multithreading in Java post.



6. How to prevent creating another instance of Singleton using Clone() method?


Singleton is all about the single instance, why there is need to create clone of it so simple and preferred way is not to implement the Cloneable interface. If somebody WANTS to implement Cloneable interface then to prevent the another instance to be created from singleton instance we can throw exception from inside the clone() method.

7. How to prevent creating another instance of Singleton using reflection?



We all know reflection can access private fields and methods so there is a chance that getting another instance created, to prevent it in the constructor we can throw the Exception.
 
private SingletonTest() { 
if( Singleton.singleton != null )  
{  
       throw new InstantiationError( "Second instance is not allowed." );  
}  
} 

8.How to prevent creating another instance of Singleton using serialization?


This is also one of the important singleton interview questions because it also shows your knowledge of serialization and deserialization. During serialization we know that readObject always creates new instance but if we use readResolve method then it doesn’t create new instance.
  
//readResolve method to prevent another instance of Singleton 
    private Object readResolve(){ 
        return INSTANCE; 
    } 
 

Best way to avoid multiple instances of singleton in serialization is to use Enum Singleton.

9. Where we use Singleton Design pattern?


In case of simple logger system we can use Logger class as singleton.
In cache mechanism, we can use the cache as a singleton object because it have a global point of reference. Also often factories are also implemented as Singleton. In simple UI application we can have main frame as singleton as well.


10 . Why can't use static class as Singleton?


This question might be asked as what is the difference between Singleton and static class or what are the advantages of singleton over the static class etc.
Singletons can implement interfaces or extend classes but static class cannot. Also static class has all the members as static but Singleton this is not the case.

Thus we have seen 10 Singleton Pattern Interview Questions and answers.If you like to add more questions, they are most welcomed by comments :)
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment