Shallow Copy and Deep Copy

Reviewed by The Kiran Academy technical training team · Updated 13 September 2026 · Current Java

Shallow copy vs deep copy in Java

A shallow copy creates a new outer object but reuses references to nested objects. A deep copy also creates independent copies of nested mutable objects, so changing one object graph does not change the other.
Shallow copySuitable when referenced objects are immutable or intentionally shared.
Deep copyNeeded when the copy must own independent mutable state.
Modern Java choicePrefer explicit copy constructors, factories or immutable records.
Use clone() carefullyCloneable is legal but often harder to maintain.
Employee(Employee source) {
  this.name = source.name;
  this.address = new Address(source.address);
}
Common mistake: new ArrayList<>(oldList) copies the list structure only; mutable elements inside remain shared.

Which copying strategy should you use?

StrategyNested mutable dataBest use
Reference assignmentEverything sharedTwo variables intentionally point to the same object
Shallow copyReferences sharedImmutable elements or deliberate sharing
Deep copyCopied recursively as designedIndependent editing of the copied graph
Immutable value objectSafe to sharePreferred when the domain can be modeled immutably

There is no universal automatic deep copy in Java. You must define what “independent” means for the domain. Database connections, files, threads and caches usually should not be duplicated even when ordinary value fields are copied.

Frequently asked questions

Does Object.clone() make a deep copy?

No. Its standard behavior is field-for-field copying, so object references still refer to the same nested objects unless your class performs additional copying.

Is Cloneable enough to clone an object?

No. Cloneable is only a marker understood by Object.clone(); it does not declare a public clone method.

Is serialization a good default deep-copy technique?

Usually not. It adds constraints and overhead and can hide the intended copy semantics. Prefer an explicit copy constructor or factory.

Primary references: Java Object.clone() and Java Cloneable.

  • Cloning is a process of creating an exact copy of an existing object in the memory.

  • In java, clone() method of java.lang.Object class is used for cloning process. This method creates an exact copy of an object on which it is called through field-by-field assignment and returns the reference of that object.

  • Not all the objects in java are eligible for cloning process. The objects which implement Cloneable interface are only eligible for cloning process.

  • Cloneable interface is a marker interface which is used to provide the marker to cloning process.

Both shallow copy and deep copy are related to this cloning process. The default version of clone() method creates the shallow copy of an object. To create the deep copy of an object, you have to override the clone() method. Let’s see how these shallow copy and deep copy work.

The default version of clone() method creates the shallow copy of an object. The shallow copy of an object will have exact copy of all the fields of original object. If original object has any references to other objects as fields, then only references of those objects are copied into clone object, copy of those objects are not created. That means any changes made to those objects through clone object will be reflected in original object or vice-versa. Shallow copy is not 100% disjoint from original object. Shallow copy is not 100% independent of original object.

Below is the example which creates the shallow copy of an object ‘jbkemployee1’.

JbkJob.java


JbkEmployee.java


JbkShallowCopy.java


In the above example, ‘jbkemployee1‘ is an object of ‘JbkEmployee‘ class which has three fields – id, name and job. ‘Job‘ is a reference variable pointing to a ‘JbkJob‘ type object. Clone of ‘jbkemployee1 ‘ is created by calling clone method on it and assigned it to ‘jbkemployee2‘. As default version of clone method creates the shallow copy, the ‘course‘ field of both ‘jbkemployee1 ‘ and ‘jbkemployee2‘ will be pointing to same ‘JbkJob‘ object. So, any changes made to this object through ‘jbkemployee2‘ will be reflected in ‘student1‘ or vice-versa.

Deep copy of an object will have exact copy of all the fields of original object just like shallow copy. But in additional, if original object has any references to other objects as fields, then copy of those objects are also created by calling clone() method on them. That means clone object and original object will be 100% disjoint. They will be 100% independent of each other. Any changes made to clone object will not be reflected in original object or vice-versa.

To create a deep copy of an object, you have to override the clone() method as demonstrated in the below example.

JbkJob.java


JbkEmployee.java


JbkDeepCopy.java


Difference between Shallow copy and Deep copy


Ready for structured training?

Learn Java Full Stack with real projects

Live classes, practical projects, interview preparation and placement support in one guided programme.

View Java Full Stack Course Book Free Demo