Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize a transient member of an object to a non-null default in Java

Tags:

public class MyObj implements Serializable {   private transient Map<String, Object> myHash = new HashMap<String, Object>();   ... } 

Is there any way to ensure that when an object of the above class is deserialized the member myHash will be set to a new empty Map rather than be set to null?

like image 543
ʞɔıu Avatar asked Jan 27 '11 00:01

ʞɔıu


1 Answers

public class MyObj implements Serializable {     private transient Map<String, Object> myHash = new HashMap<String, Object>();      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {         in.defaultReadObject();          myHash = new HashMap<String, Object>();     } } 
like image 177
timon Avatar answered Nov 10 '22 06:11

timon