java - copying objects with data fields -
this question has answer here:
- how copy object in java? 20 answers
i need copy content of class object new class object. don't need copy references data, because in case if change fields in object a2 data changed in object a2.
public class msg { string info=""; hashmap fld = new hashmap(); public void assign(msg value) { info = value.infol; fld = value.fld; } } // stuff class a1 = new msg(); a1.info="111"; a1.fld.put("1","111"); a2 = new msg(); a2.assign(a1); how realise function assign?
you need make copy of hashmap or have 2 objects pointing same hashmap.
public void assign(msg value) { info = value.infol; fld = new hashmap(value.fld); }
Comments
Post a Comment