Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created February 14, 2016 21:11
Show Gist options
  • Save deyindra/772a6ecd032e23723627 to your computer and use it in GitHub Desktop.
Save deyindra/772a6ecd032e23723627 to your computer and use it in GitHub Desktop.
Singleton Deserialization
import java.io.*;
public class Singleton implements Serializable {
private static volatile Singleton object;
private Singleton() {
}
public static Singleton getInstance() {
if (object == null) {
synchronized (Singleton.class) {
if (object == null) {
object = new Singleton();
}
}
}
return object;
}
//If Implement Serialization then it should return the same singleton object
protected Object readResolve() throws ObjectStreamException {
return Singleton.getInstance();
}
}
@devopam
Copy link

devopam commented Feb 15, 2016

helpful and quite useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment