How do I copy an object in Java?

Create a copy constructor: class DummyBean { private String dummy; public DummyBean(DummyBean another) { this.dummy = another.dummy; // you can access } } Every object has also a clone method which can be used to copy the object, but don’t use it. It’s way too easy to create a class and do improper clone method. … Read more

How do I clone a list so that it doesn’t change unexpectedly after assignment?

With new_list = my_list, you don’t actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment. To actually copy the list, you have various possibilities: You can use the builtin list.copy() method (available since Python … Read more