简体   繁体   English

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

[英]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. 我认为 AObj 将只有一个副本,除非我们在新的 object 中添加任何更改或更新最初创建的 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.您不要复制 object。 Your code does what you want.你的代码做你想做的事。

(It is pretty difficult to accidentally copy something in Java.) (在 Java 中意外复制某些内容非常困难。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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