aspose file tools
The moose likes Java in General and the fly likes Why not a class with static methods as replacement for Singleton object? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why not a class with static methods as replacement for Singleton object?" Watch "Why not a class with static methods as replacement for Singleton object?" New topic
Author

Why not a class with static methods as replacement for Singleton object?

Pushker Chaubey
Ranch Hand

Joined: Dec 06, 2006
Posts: 53
Hi Experts,

I have seen this question asked in an interview.

Why we cannot use a class with static methods (and possibly with private constructor) as replacement for Singleton?

Please share your views.

regards,
Pushker chaubey
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Good question. My 2 cents is that in some cases a class with static methods would work out fine. But if you use an interface to describe the behavior of the singleton, you need an object to provide the implementation.
Pushker Chaubey
Ranch Hand

Joined: Dec 06, 2006
Posts: 53
Hi Max,

Thanks for a quick reply.


regards,
Pushker chaubey
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If your singleton class implements an interface or extends another class, then without the singleton instance you will not be able to use your class as a parameter or return value. A good example is String.CASE_INSENSITIVE_ORDER - a singleton Comparator<String> so you can use it in Collections.sort for instance. Another example is Collections.EMPTY_SET and its brothers - singleton Set, List etc implementations you can use as a return value if you have nothing to return:
This way, if you know that the complex steps are not necessary because you're going to return an empty Set anyway, you return the singleton which avoids the creation of a new object.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8430

If the class has static methods, why do you even need to care if the constructor is public or private?


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
Maneesh Godbole wrote:If the class has static methods, why do you even need to care if the constructor is public or private?

Code clarity, you'de be presenting a way to users of your code to instantiate a class that has no non-static methods. Private constructors remove that option and as a result make developers use the class the way it was intended. That said I have a preference for the Singleton pattern even if an all static method class would do as well.
Pushker Chaubey
Ranch Hand

Joined: Dec 06, 2006
Posts: 53
Thanks guys!
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 341
R van Vliet wrote:Code clarity, you'de be presenting a way to users of your code to instantiate a class that has no non-static methods. Private constructors remove that option and as a result make developers use the class the way it was intended


Note that this doesn't apply to abstract classes, since their subclasses may well include non-static methods.

http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Why not a class with static methods as replacement for Singleton object?
 
Similar Threads
singleton vs static methods
Differences bet a singleton and class w/ private constructor & public static methods
Singleton / Factory decision
Synchronization for Singleton
Utility Classes in EJB