Home Java Beginners Implementing a Serializable Singleton



Implementing a Serializable Singleton
Posted on: June 7, 2007 at 12:00 AM
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 java.io.Serializable 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:

 

 

import java.io.*;

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

Download this program

Related Tags for Implementing a Serializable Singleton:
javacclassinterfaceiomethodserialreadintriathisserializetoeilserializablecanliuseimceinjava.ioasmntcajadaceclesemmedoresolvessoeeatishallmplgoeaarvazssrithavabablhatfacepleplonolo


More Tutorials from this section

Ask Questions?    Discuss: Implementing a Serializable Singleton   View All Comments

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.