• 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

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

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Max,

Thanks for a quick reply.


regards,
Pushker chaubey
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the class has static methods, why do you even need to care if the constructor is public or private?
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic