Share on Google+Share on Google+

Implementing a Serializable Singleton

In Singeton classes only one instance will be created.

Implementing a Serializable Singleton


In Singeton classes only one instance will be created. We are going to serialize the class. This can be done very easily. What we need to do is to implement a interface. We will use a method of Serializable interface that is readResolve().

readResolve(): It returns Object and throw ObjectStreamException


Code of the program is given below:




public class SerializableSingleton implements {
 static SerializableSingleton singleton;
 protected SerializableSingleton() 
  // Exists only to thwart instantiation.
  private Object readResolve() 
 return instancd;
  public static void main(String args[])
  singleton = new SerializableSingleton();

Download this program



Posted on: June 7, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Implementing a Serializable Singleton   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image