Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static vs non-static methods inside a Singleton?

 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have a DAO class which is spring managed. I need to decide whether to use static/non-static methods. I believe using non-static methods will force callers of the DAO to wait until it is fully initialised, and is the better option, since you have a fully formed class. please let me know what are the factors that I must consider before deciding?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want a singleton in the first place?
You should be initialising the object via its constructor, without letting a “this reference” escape, Then nobody can get anywhere near the object until it is fully initialised. In which case you would probably not want static fields.

I am sure other people will have different opinions.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since that's a DAO managed by Spring container, you don't need to worry about partially initialized object. Spring instantiates and initializes all the beans on startup (there are of course way of doing lazy-initialization), but that doesn't make any difference, because in that case also, Spring will ensure you don't get partially initialized object. So, you should not add static method, not for at least this purpose.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic