aspose file tools*
The moose likes Java in General and the fly likes static vs non-static methods inside a Singleton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static vs non-static methods inside a Singleton?" Watch "static vs non-static methods inside a Singleton?" New topic
Author

static vs non-static methods inside a Singleton?

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
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

Joined: Oct 13, 2005
Posts: 38392
    
  23
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

Joined: Aug 11, 2012
Posts: 372
    
    1

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.
 
 
subject: static vs non-static methods inside a Singleton?