简体   繁体   中英

How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java


Please advise. I think AObj will have only one copy unless we add any changes or update the originally created object within new object. Not sure, please help.
 public class AObject { private String a; Private String b; public String getA() { return a; } public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } } } public class BObject{ private AObject aObj; public AObject getAObj() { return aObj; } public void setAObj(AObject aObj) { this.aObj = aObj; } } public static void main(String[] args){ AObject aObj = new AObject(); aObj.setA("testA"); aObj.setB("testB"); BObject bObj = new BObject(); bObj.setAObj(aObj) //here I would like to avoid creating the deep copy of objects. }

You do not copy the object. Your code does what you want.

(It is pretty difficult to accidentally copy something in Java.)

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