How do I copy an object in Java?

后端 未结 23 2609
终归单人心
终归单人心 2020-11-21 04:50

Consider the code below:

DummyBean dum = new DummyBean();
dum.setDummy(\"foo\");
System.out.println(dum.getDummy()); // prints \'foo\'

DummyBean dumtwo = du         


        
23条回答
  •  孤街浪徒
    2020-11-21 05:35

    To do that you have to clone the object in some way. Although Java has a cloning mechanism, don't use it if you don't have to. Create a copy method that does the copy work for you, and then do:

    dumtwo = dum.copy();
    

    Here is some more advice on different techniques for accomplishing a copy.

提交回复
热议问题