Tuesday, 17 September 2013

How to create a Singleton

How to create a Singleton

Today in my interview one interviewer asked me to write a Singleton class.
And i gave my answer as
public class Singleton {
private static Singleton ref;
private Singleton() {
}
public static Singleton getInstance() {
if (ref == null) {
ref = new Singleton();
}
return ref;
}
}
suddenly he told me this is old way of writing the class. Can any one
please help me why he told like that. i will be very obliged.

No comments:

Post a Comment