aspose file tools*
The moose likes Java in General and the fly likes Singleton Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton Question" Watch "Singleton Question" New topic
Author

Singleton Question

Hrishikesh Ghatnekar
Greenhorn

Joined: Oct 25, 2004
Posts: 28
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;
}
}



}

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The "getInstance()" method of a Singleton class is supposed to be static, as it's used to get a reference to the singleton instance. But all the other methods are supposed to be instance (i.e., non-static) methods -- otherwise, the single instance is itself useless, and instead you've just got a class with all static methods.


[Jess in Action][AskingGoodQuestions]
Hrishikesh Ghatnekar
Greenhorn

Joined: Oct 25, 2004
Posts: 28
Yes, Very Much correct.

Thanks for the help


:-)
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75


=> this will only work with static methods of your class (a not very OO approach)



=> this makes sure your Singletone instance has been constructed and that initialization has occured before you call methodName(). You have more control with this approach.


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
hello Hrishikesh ,hope this helps you

class Singleton
{
private static Singleton instance;

private Singleton()
{
}

public static Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
return instance;
}

return instance;
}

}

Cheers



I have always depended on the kindness of strangers....
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by faisal usmani:
hello Hrishikesh ,hope this helps you


Not so much; you took his thread-safe version and made it not thread-safe; not clear to me why you offered this.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
OOps forgot to make it thread safe !!!

class Singleton
{
private static Singleton instance;

private Singleton()
{
}

public static synchronized Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
return instance;
}

return instance;
}

}


Does this makes sense with respect to thread safety :roll:

regards
[ April 18, 2006: Message edited by: faisal usmani ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Singleton Question