• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton Question

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}



}

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hrishikesh Ghatnekar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Very Much correct.

Thanks for the help


:-)
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


=> 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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic