IntelliJ Java IDE
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton vs static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Singleton vs static methods" Watch "Singleton vs static methods" New topic
Author

Singleton vs static methods

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 90
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
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

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...).


S.D. MADHAN
Not many get the right opportunity !
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 12513

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.


One of the most odious afflictions that Business has inflicted on the modern English language is "pro-active". Most of the time it's simply redundantly used in place of the simple old word "active". And a good deal of the rest of the time it means "You're not overworked enough yet, so go out and find more!"
 
IntelliJ Java IDE
 
subject: Singleton vs static methods
 
Threads others viewed
Singleton versus Static class?
Regsrding the use of singleton method in our assignment.....
When do I need to be concerned with threading?
singleton objects?
Understanding singleton pattern
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture