Singleton Question

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;

}

}

}

[1501 byte] By [Hrishikesh_Ghatnekara] at [2007-10-2 17:51:33]
# 1

> In Second case how come the program work without

> going thorough the constructor?

>

The method is static, remove that keyword and you will need an instance of the object to call the method on: http://www.google.co.za/search?hl=en&q=%2Bjava+%2Btutorial&meta=

Then to find out what the singleton pattern is:

http://www.google.co.za/search?hl=en&q=%2Bjava+%2B%22singleton+pattern%22&meta=

MichaelW13a at 2007-7-13 19:09:53 > top of Java-index,Other Topics,Patterns & OO Design...