简体   繁体   中英

Is there a way to serialize and deserialize a Singleton class in java keeping its singleton nature?

How we can serialize/deserialize a class in java with keeping its singleton nature in the application. Suppose I have serialized a class on every re-deployment of my application with updated values which i want to deserialize later.

yes you can do it my implementing

1) instance of the class as static

public static Singleton instance = new Singleton();

2) you have to add private constructor

private Singleton()  
{ 
    // private constructor 
} 

3) You have to declare method by which you can access the declared obj

// implement readResolve method 
protected Object readResolve() 
{ 
    return instance; 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM