Anuj Panwar
Anuj Panwar

Reputation: 113

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.

Upvotes: 0

Views: 265

Answers (1)

Rohit RC
Rohit RC

Reputation: 469

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; 
} 

Upvotes: 1

Related Questions