import java.io.ObjectStreamException;
import java.io.Serializable;
/**
* Singleton with final field, static factory and readResolve method
* to care when the singleton is de-serialized.
*/

public class Item2 implements Serializable {
    public static final Item2 INSTANCE = new Item2();

    private Item2() {
        System.out.println( "Item2 constructor called");
    }

    // factory method to access the instance
    public static Item2 getInstance() {
        return INSTANCE;
    }

    // readResolve method to preserve singleton property
    private Object readResolve() throws ObjectStreamException {
    /*
    * Return the one true Item2 and let the garbage collector
    * take care of the Item2 impersonator.
    */
        return INSTANCE;
    }
}
 

Hosted by www.Geocities.ws

1