my dog learned polymorphism
The moose likes Java in General and the fly likes singleton class 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 class" Watch "singleton class" New topic

singleton class

kesava chaitanya

Joined: Aug 20, 2001
Posts: 28
can any one tell me wat is singelton classs with one simple example.
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
Originally posted by kesava chaitanya:
can any one tell me wat is singelton classs with one simple example.

Hi. This example comes straight out of my book (Item 2, Enforce the singleton property with a private constructor):
//Singleton with final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();

private Elvis(){

...//Remainder omitted
The key to this pattern is the coupling of a public static field with a private constructor. (The public field may be replaced by a public static factory.) The sole instance is created at class initialization time. There is no way that a client can ever create a second instance.
The one caution concerns serialization. If a singleton is made serializable, it is imperative that a readResolve method be provided to prevent the creation of additional instances via deserialization:
//readResolve method to preserve singleton property
private Object readResolve()throws ObjectStreamException {
* Return the one true Elvis and let the garbage
* collector take care of the Elvis impersonator.
return INSTANCE;

Joshua Bloch
Author of Effective Java
[This message has been edited by Joshua Bloch (edited August 22, 2001).]

Joshua Bloch <br />Author of <a href="" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="" target="_blank" rel="nofollow">Java Puzzlers</a>
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
A "Singleton" is a design pattern where
their there is only a single instance of a class.
If you are trying to create a class which
represents you,logically speaking there should be only instance of this class, as there is only one "you".Therefore there is no
point of creating more that one instances of
"your class".This is acheived with the
singleton design pattern.

Well , i think the proper way of doing that is keeping the instance private( as any other attribute of a class) and accessing it through a public static method, unlike the way it is shown above.This way we protect
the instance from outside.
Like this..
public class SingleTonClass {
//create the instance and declare it as private and static
private static SingletonClass obj = new SingleTonClass();
private SingleTonClass()//prevent creation of objects from
{ } //outside this class
public static SingleTonClass getInstance()
return obj;
/*getInstance() is the static method which will return the instance.*/

[This message has been edited by Manjunath Subramanian (edited August 23, 2001).]
I agree. Here's the link:
subject: singleton class
jQuery in Action, 3rd edition