• 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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if I have the following:


When declaring a singleton class, is it wrong to declare member variables? I say no because those member variables only occur once per class. Someone said "never put member variables for static classes".

Thanks.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gallen Thomas wrote:When declaring a singleton class, is it wrong to declare member variables? I say no because those member variables only occur once per class. Someone said "never put member variables for static classes".


But you don't have a static class, you have an enum, so if it needs a member variable, you have no choice. What you should definitely avoid are static variables.

My question is: do you need a singleton? A much-abused pattern, as you'll discover if you Google "singletons are evil".

Winston
 
Gallen Thomas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston for replying.

I agree with you that I don't have a static class but my colleague keeps calling it a static class. I keep telling them its not a static class. oh well. Yes, I was correct to question/tell my colleague that there is nothing wrong with having a member variable.

I will google "singletons are evil"

Thank you again!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:But you don't have a static class, you have an enum, so if it needs a member variable, you have no choice...


Actually, thinking about it, an enum is a bit like a static class (by which I assume you mean a class with a single private no-args constructor), so declaring a member variable would be a bit like declaring a static variable.

Enums are generally about defining constants, not variables. If they have state, you're back to the basic issue about singletons: Global state is usually not a good thing. At the very least you will almost certainly need to make the class Thread-safe, since anyone can use it.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gallen Thomas wrote:Thank you again!


You're welcome. It's a tricky one, because singletons seem like a really good idea. The problem comes when you want to substitute something different - in particular for things like testing. If you are going to use an enum this way, at the very least I would suggest that you have it implement an interface which defines all the behaviour you want. Then you can define other instances to cover situations for stuff like testing or simulations, viz:
MyEnv env = MyClass.PROD;
(assuming that MyEnv is an interface implemented by your MyClass enum)

Good luck.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic