aspose file tools*
The moose likes Java in General and the fly likes Singleton versus Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton versus Static Methods" Watch "Singleton versus Static Methods" New topic
Author

Singleton versus Static Methods

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 132
Hi,

Singleton is having one instance per JVM. And, similar functionality we can achieve using a class with static variables and static methods. So, what is the basic difference and use case for using a singleton and static methods.

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Chalk and Cheese.

A singleton is a true object, which can have instance fields and instance methods. Most people nowadays think singletons are by no means as good as they were once thought.
A utility class is usually uninstantiable, and would have no instance fields and instance methods.
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 132
Campbell Ritchie wrote:Chalk and Cheese.

A singleton is a true object, which can have instance fields and instance methods. Most people nowadays think singletons are by no means as good as they were once thought.
A utility class is usually uninstantiable, and would have no instance fields and instance methods.


Thanks Campbell for your response.

But I am wondering do we really need instance variables/methods while we are referring to Singleton. Since we just need one instance shared across the application objects. So, it really doesn't depend upon any object state. Please correct me if I am misunderstood.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vaibhav G Garg wrote:But I am wondering do we really need instance variables/methods while we are referring to Singleton. Since we just need one instance shared across the application objects.


So? That doesn't mean it won't have state.

More to the point though are the methods. In particular, there's no runtime polymorphism of static methods. So if we want to use different implementations of that class, depending on runtime needs, we can't do that with a bunch of static methods.

And finally, one might choose a singleton for design and documentation reasons. Namely, to make it clear that in your design you view that as an object, and that there is exacly one of them, rather than as a bunch of functions that happen to be grouped together.
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47
Singleton ensures that any number of calls made to that particular class would just return the same Instance and not a new copy of each Instance is created for every single call made.


Static methods ensure that no new object need to be created for accessing the method of the class. Just the class name.method name would facilitate us in gaining access to the method of the class. Object instance need to be created when accessing static content say static variable or static method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Balaji Vankadaru wrote:Singleton ensures that any number of calls made to that particular class would just return the same Instance and not a new copy of each Instance is created for every single call made.


Static methods ensure that no new object need to be created for accessing the method of the class. Just the class name.method name would facilitate us in gaining access to the method of the class. Object instance need to be created when accessing static content say static variable or static method.


That doesn't address the question at all. That merely reiterates what the OP clearly already knows.

In the future, please do read the question and existing replies more carefully before supplying your own response.
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 132

Jeff Verdegan wrote:
So? That doesn't mean it won't have state.




Does it really significant to have a state i.e. instance variables in a singleton object? The whole application uses the singleton as a shared object so how can it have a state corresponding to different instance variables?

Jeff Verdegan wrote:
More to the point though are the methods. In particular, there's no runtime polymorphism of static methods. So if we want to use different implementations of that class, depending on runtime needs, we can't do that with a bunch of static methods.


If we have just one common logic/method, then won't the singleton and static method of a class be serving the same purpose? What will be preferential in that scenario?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vaibhav G Garg wrote:
Jeff Verdegan wrote:
So? That doesn't mean it won't have state.




Does it really significant to have a state i.e. instance variables in a singleton object? The whole application uses the singleton as a shared object so how can it have a state corresponding to different instance variables?



It depends on what that singleton's job is. Even though there's only one of it, it can still be an object that maintains its own state. For instance, maybe it's a connection manager for a database. Its state might include a pool of connections.

Jeff Verdegan wrote:
More to the point though are the methods. In particular, there's no runtime polymorphism of static methods. So if we want to use different implementations of that class, depending on runtime needs, we can't do that with a bunch of static methods.


If we have just one common logic/method, then won't the singleton and static method of a class be serving the same purpose? What will be preferential in that scenario?


I'm talking about the scenario when we don't have "just one common logic/method."

This singleton or class full of static methods is meant to do a job. Just like anything else that is meant to do a job, there might be different ways of doing it, and we might want to decide at runtime which way to use.

Let's say we have some design where we have an interface DatabaseConnection, and maybe some or all of its implementations are singletons. At runtime, we decide which DatabaseConnection implementation to use.


If those implementation classes are singletons--actual objects--we can do that. If they're just full of static methods, we can't.
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 132
Thank you Jeff for your quick response.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Vaibhav G Garg wrote:
... won't the singleton and static method of a class be serving the same purpose? What will be preferential in that scenario?

Also, there is possibility to reference your object from a class with soft/weak reference, so the whole state can be dropped. In case of static classes you need to nullify all the references.

Singleton is having one instance per JVM

I think this isn't true. Every classloader can have it's own singleton.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton versus Static Methods
 
Similar Threads
Design Pattern
customized constructors
Singleton Class
singleton vs static
Synchronization for Singleton