• 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 vs static methods

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

I would like to know about pros n cons of Singleton and class with static methods and where to use what?

Also, What I could find is, Singleton should be used where we want to have some state associated, but with static methods too, we can have static variables to keep the state as with singleton also only one instance is involved?

Thanks,
Sunila
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton pattern ensures that others get hold of a single instance of an entity.

Static pattern, though is maintained as a single copy in JVM, could result in concurrency issues (when used in replication/fault-tolerant scenarios), if data members are also accessible or they manipulate external resources (such as files, databases etc...).
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In terms of practical experience - as opposed to theory - I've usually regretted using static methods. Sooner or later, I tend to end up with a situation where the bean in question needs to maintain state and the state isn't global. It's fairly easy to convert singletons to non-singleton objects, but converting static stateless to non-singleton stateful objects can be a right royal pain.
 
reply
    Bookmark Topic Watch Topic
  • New Topic