Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton class

 
kesava chaitanya
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
can any one tell me wat is singelton classs with one simple example.
bye
chaitu
 
Joshua Bloch
Author and "Sun God"
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Manjunath Subramanian
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic