• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Singleton Objects

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I know about the singleton objects and how we can achieve this.
My question is If I make all the members and methods of my class as static then there will be only one copy of them crearted.
Please help me to understand when I should go for Singleton Object? and what are the advantages/disadvantages over the class as I describe above?
Thanks,
Sachin
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q) I know about the singleton objects and how we can achieve this.
A)
One of the way to achieve your objective is
1. Declare the constructor as private static final.
2. Declare one method as static with return current class (example getInstance as shown.)
3. create required methods as per the requirement.
public SingletonTest{
private static final SingletonTest SINGLETON = new SingletonTest();


priavate SingletonTest()
{}
public static SingletonTest getInstance()
{
return SINGLETON;
}

public TestPrint(String strName)
{
System.out.println("Name passed is "+ strName);
}

}

Q)what are the advantages/disadvantages over the class as I describe above?
Many times you might require only one instance of class is made used by a application during its execution.
Example one part of the application set some value to the class variable and some other point of time another part of application want the value same value set. Since same instance is serviced for all request thought out the application execution. This can be easily achieved by following Singleton Object.

Using singleton model when you really don�t require it and ending up in a mess is the disadvantage part which will not occur if it is understood and used.
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, singleton instances can implement interfaces, be extended, etc - something that is not easy with classes containing static methods.
Moving this to the Patterns forum...
Simon
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also beware that the Singleton pattern (and the degenerate case of using static methods) have a lot of drawbacks. Do a search in this forum for "singleton" and you will finda whole lot of discussion about all these points.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic