wood burning stoves 2.0*
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
Author

singleton class

kesava chaitanya
Greenhorn

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

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

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

private Elvis(){
...
}

...//Remainder omitted
}
</pre>
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:
<pre>
//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;
}
</pre>
Regards,

------------------
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="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>
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).]
 
GeeCON Prague 2014
 
subject: singleton class