File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes 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 "Singleton" Watch "Singleton" New topic
Author

Singleton

Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi
How do you make a class to be singleton? What does this mean. Can u explain it?

Arathi
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Arathi,

See "Effective Java" Item 2. It's in Chapter 2, "Creating and Destroying Objects." I think that chapter is available online.

Josh


Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Originally posted by Joshua Bloch:
Arathi,

See "Effective Java" Item 2. It's in Chapter 2, "Creating and Destroying Objects." I think that chapter is available online.

Josh


Hi Couldn't find the sample chapter on Creating and Destroying Objects. If possible Explain the concept.

Thanks
Arathi
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by Arathi Raj:
Hi
How do you make a class to be singleton? What does this mean. Can u explain it?

Arathi


A singleton class is which will have only one instance for the entire application. It means there will be only one Object of a particulat class can be created in a application.

You implement this by making the constructor as private. Sample code looks like this:
public class SingletonClass {
private static SingletonClass singletonClass = null;

private SingletonClass() {
// restricts the user not to create object
}

public static SingletonClass getSingletonClass() {
if(singletonClass == null) {
singletonClass = new SingletonClass();
}
return singletonClass;
}
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Arathi Raj:
Hi
How do you make a class to be singleton? What does this mean. Can u explain it?

Arathi


Try googling for "Singleton". The first few links give decent descriptions. If you still don't understand, come back with specific questions about the problems you encountered. We will be happy to clarify from there.

Layne


Java API Documentation
The Java Tutorial
fdafd fdafda
Greenhorn

Joined: Aug 09, 2005
Posts: 6
I found the wikipedia explanation quite easy to understand

http://en.wikipedia.org/wiki/Singleton_pattern
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
FYI: Singleton looks interesting in some aspects. But, may need to be programmed with care, if you are introducing aspects like hashmaps inside it and making the hashmap bigger over the time. That can make it a memory hog.


Kishore
SCJP, blog
 
 
subject: Singleton