Shallow Copy example

In this example we shall show you how to create a shallow copy of a class. To create a shallow copy of a class we have performed the following steps:

  • We have created a class, Car that has a String field and a getter and setter method for it. It also has a constructor using its String field.
  • We have also created a class, Person, that implements the Cloneable interface in order to override its clone() API method. It has a Car field and a String field, getters for both fields and a setter method for the String field. It also has a constructor using both fields, where it initializes the Car field with a given String and sets the value of the String field to another given field.
  • In clone() method the super classe’s clone() method is called, that is the clone() method of Object.
  • We create a new instance of Person and using its getters we get the String field and the String field of its Car field.
  • We also create a clone object, using the clone() method of Person.
  • We change the String field of clone object and the String field of Car field of the clone object. Both fields are changed in the clone object, whereas the original objects has held its initial value for its String field, and the String field of its low-level object Car has also changed,

as described in the code snippet below.  

package com.javacodegeeks.snippets.core;

public class ShallowCopy {

    public static void main(String[] args) {

  //Original Object

  Person person = new Person("Person-A", "Civic");

  System.out.println("Original : " + person.getN() + " - "

    + person.getC().getN());

  //Clone as a shallow copy

  Person person2 = (Person) person.clone();

  System.out.println("Clone (before change): " + person2.getN() + " - "

    + person2.getC().getN());

  //change the primitive member


  //change the lower-level object


  System.out.println("Clone (after change): " + person2.getN() + " - "

    + person2.getC().getN());

  System.out.println("Original (after clone is modified): " + person.getN()

    + " - " + person.getC().getN());


class Person implements Cloneable {
    //Lower-level object

    private Car carObject;
    private String name;

    public Car getC() {

  return carObject;

    public String getN() {

  return name;

    public void setN(String s) {

  name = s;

    public Person(String s, String t) {

  name = s;

  carObject = new Car(t);

    public Object clone() {

  //shallow copy

  try {

return super.clone();

  } catch (CloneNotSupportedException e) {

return null;


class Car {

    private String carName;

    public String getN() {

  return carName;

    public void setN(String s) {

  carName = s;

    public Car(String s) {

  carName = s;


Original : Person-A - Civic
Clone (before change): Person-A - Civic
Clone (after change): Person-B - Accord
Original (after clone is modified): Person-A - Accord

This was an example of how to create a shallow copy of a class in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button