• 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

When Referencing class is it better to use static methods or Singleton Interface

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the implication on both aproach and which one is better to adapt as coding approach.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best answer I can give is, "it depends." [NOTE: If your question refers to whether it's better to access a static method using the class name or an instance, then ignore my response. I prefer to use the class name in that case.]
If you are writing a class that consists of only "service" or "convenience" methods, you might want to define a class that contains only static methods. You can use the "java.lang.Math" class as a model of how to do this. All the static variables (PI and E) are final so you don't have to worry about two threads updating them simultaneously. However you should declare a private constructor--that does nothing--to ensure that no one can instantiate the class.
Otherwise I'd lean towards the singleton approach. You can have state, you can override useful methods (such as "toString()"), you can extend the class as necessary and have normal, polymorphic behavior. [static methods don't understand "this" or "super"]. Later, if you decided you need a "pool" of these variables, it's very easy to add that behavior to the class.
I'd like to hear the thoughts of others as well, but I tend to go with the "singleton" except in exceptional cases.
[ January 14, 2004: Message edited by: Wayne L Johnson ]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also an efficiency factor: a singleton class is only instantiated
the first time it's used and only once: therafter , THIS instance will be used by all successive calls.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Wayne...what you essentially mean is: For convenience methods write a static class while for getting polymorphic behaviour or maintaining a state use Singleton.
But usually its not recommended to extend a Singleton in apprehension of accidental creation of two instances of a Singleton.
Also I could not appreciate the efficiency factor stated by Pierre. If the method is static then a call to it does not demand the instantiation of its class every time.
May be I am simplifying the question or adding a bit to it but can someone elaborate the pros and cons of using a Singleton over a static class???
Thanks,
Gaurav
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav
I am also bit confused sometimes whether to use Static mentods on a class or use a singleton class. They both serve the purpose.
But you should use the singleton class if there is any chance in the future to use more then one instance and still have the control on the creation of instances. eg. we have a singleton ManagerClass and it is sufficient to serve our purpose, but in cas in the future the workload increases we can easily let our singleotn method eg getManager use two(or configurable no.) instances to serve the clients. It also helps if we have synchronized methods which have become a bottleneck. then you can create two instances of the class and can have two concurrent processes at the same time. Yes it will be obviously no more a Singleton class. But using singleton we can easily leave singleton approach and use multiple instances. Which we can not do while using static methods on a class.
Cheers
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you have a very good reason I would use neither static methods or a singleton. Instead consider using normal objects. If you're thinking of effeciency then you are optimising prematurely.
The problem with static methods is that they cannot be overriden which makes your design less flexible.
The problem with Singletons are again that they reduce flexibility. They cannot be subclassed properly, can cause horrific class interdependecies (global variables anyone ?) and can render systems very difficult to test. If you search this site for Singleton esp. the "OO, Patterns, UML and Refactoring" forum and look in the Java FAQ there is much more information.
hope this helps,
Don
 
Gaurav Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, that helps.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic