File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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

Joined: Oct 13, 2005
Posts: 46368
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: 375

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.
I agree. Here's the link:
subject: static vs non-static methods inside a Singleton?
It's not a secret anymore!