This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

singleton class  RSS feed

 
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
 
Author and "Sun God"
Ranch Hand
Posts: 185
9
  • 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).]
 
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!