wood burning stoves*
The moose likes Java in General and the fly likes Singleton Pattern in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton Pattern in java" Watch "Singleton Pattern in java" New topic
Author

Singleton Pattern in java

Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,


Could you tell me examples where singleton pattern is used in java programming apart from logger?

Thanks
Padma priya N.G.
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
Singleton class are those whose only instance exists in the application. Multiple instance would make no sense.
In Singleton class the constructor are not exposed, that means the default contructor is made private.
The only way to ge the instance is by calling the exposed stativ method which will return the same instance every time.

In JDK there are many example of Singleton class
Take example of java.lang.Runtime class

You can get the reference of Runtime class by calling



What Runtime class provides you can explore through Java API

SCJP 5.0 OCBCD 5.0(Formerly SCBCD)
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I dont think singletons are good practice. In many if not all of the cases i implemented them i had to change them back because i needed more than one instance.
Even if you realy think you don't need more than one you should implement it in a non static way so you can easily change it later.

Please correct my English.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Manuel Petermann wrote:I dont think singletons are good practice.


Wrong. Singletons are extremely good practice if you know how to use them.
In one of my applications, I use a singleton to store the application's settings - why should I have many copies or read them every single time a user loggs on, if I can just store them in a singleton?

Singletons are used whenever you need one, and exactly one, object of something.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Jan Hoppmann wrote:Wrong. Singletons are extremely good practice if you know how to use them.
In one of my applications, I use a singleton to store the application's settings - why should I have many copies or read them every single time a user loggs on, if I can just store them in a singleton?

Singletons are used whenever you need one, and exactly one, object of something.

I disagree. Singletons are almost always not good practice. One of the problems with singletons are that they make your code hard to test. Singletons, implemented with the classic static instance pattern, are hard to replace by mock or stub implementations, which is what you very often want to do in unit tests. You can use a dependency injection framework to get around this (when testing, you let the framework inject mocks instead of the "real" objects).

You can find a lot of discussion about why many people regard singletons as bad by searching for "why singletons are bad" or similar search terms.

Especially singletons which have (mutable) state (member variables) are bad, because those variables are essentially global variables, which can cause all sorts of issues, for example with thread safety.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Jesper de Jong wrote:
I disagree. Singletons are almost always not good practice. One of the problems with singletons are that they make your code hard to test. Singletons, implemented with the classic static instance pattern, are hard to replace by mock or stub implementations, which is what you very often want to do in unit tests. You can use a dependency injection framework to get around this (when testing, you let the framework inject mocks instead of the "real" objects).

You can find a lot of discussion about why many people regard singletons as bad by searching for "why singletons are bad" or similar search terms.

Especially singletons which have (mutable) state (member variables) are bad, because those variables are essentially global variables, which can cause all sorts of issues, for example with thread safety.


Oh, okay. I hadn't thought about testing (mainly because we use dependency injection at work), but you're right.
But that doesn't mean that there are no cases where you should use a singleton, does it?

(And I should remember to use "I disagree", like you did, instead of "Wrong" - my opinion is not the only valid, as other opinions are not automatically wrong if they differ - I sincerely apologize to Manuel.)
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

No problem. I suggest reading this.
In addition i would say that if you are designing software you might think its a good idea to use singletons.
You know of their dangers. You know it but others might not so they might assume its safe do do things with your singleton you did not intent which interfere with the controlflow of the rest of the program.
You say you are using them for configurations? I really hope you made them immutuable because you might end up in errors which are really bad to debug.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Pattern in java
 
Similar Threads
Hibernate SessionFactory is initialized/read with every EJB call
using singleton pattern in java
Single pattern
Limited no of Objects
Static methods Vs "Instance Methods"