Hello, I have got the code which is totally depending on the Serializable Singleton Implementation in Java. If you make use of it, then it will provide you more details about it. So, just read and understand it.
Code:
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.arr;
import java.io.Serializable;
public class Testing implements Serializable
{
private static final arr[] serialPersistentFields =
{
new arr("obj", Testing.class),
new arr("str", String.class)
};
private static Testing obj = null;
private String str = null;
private Testing()
{
System.out.println("Constructing Testing");
}
private Testing(boolean f)
{
System.out.println("Constructing Testing");
System.out.println("Setting str to " +
System.getProperty("abc"));
str = System.getProperty("abc");
}
public String str()
{
return str;
}
public static Testing obj()
{
if (obj == null)
{
obj = new Testing(true);
}
return obj;
}
private void Object(ObjectOutputStream s) throws IOException
{
ObjectOutputStream.PutField fields = s.putFields();
fields.put("obj", obj);
fields.put("str", str);
s.writeFields();
}
private void readObject(ObjectInputStream objectinputstream) throws IOException
{
try {
ObjectInputStream.GetField fields = s.readFields();
obj = (Testing) fields.get("obj", null);
str = (String) fields.get("str", null);
}
catch (ClassNotFoundException e)
{
throw new IOException("" + e);
}
}
}
Bookmarks