0

How to create an independent copy of the object route? The problem is that all the updates applied to route_copy are also applied to route. How to avoid this?

public class Route implements Cloneable, Comparable<Route> {
//...
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
//...
}

public void processData(Route route)
{
        route_copy = null;
        try {
            route_copy = (Route) route.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
//...
}
Viktor Seifert
  • 636
  • 1
  • 7
  • 17
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

1 Answers1

2

You can provide a deep copy method in your class or a copy constructor. Check this related post to understand how to do that:

Copy constructors and defensive copying

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136