Hi,
I am trying to create one singleton class and planning to use it in my application.
My question is what will be difference if I try to access the methods of the singletone class in following ways
1)Singletone.getInstance().methodName()
2)Singletone.methodName()
In Second case how come the program work without going thorough the constructor?
The code for my program can be
public class Singltone_impl {
private static Singltone_impl validator = null;
protected Singltone_impl() {
System.out.println("In Singltone_impl constructor <=");
init();
System.out.println("Singltone_impl constructor <=");
}
protected void init() {
System.out.println("In Singltone_impl init <=");
System.out.println("In Singltone_impl init =>");
}
public static Singltone_impl getInstance() {
synchronized (Singltone_impl.class) {
if (validator != null) {
return validator;
} else {
validator = new Singltone_impl();
return validator;
}
}
}
public static boolean validate(
String email_id) {
System.out.println("In Singltone_impl validate ");
if (email_id == null || email_id.trim().length() < 1) {
System.out.println("Singltone_impl , email ID not present , returning false ");
return false;
}
else {
System.out.println("TRRRRRRRRRRUEEEEEEEEE");
return true;
}
}
}