Copy Constructor in Java

Filed Under: Java

Copy Constructor in java class is a special type of constructor that takes same class as argument. Copy constructor is used to provide a copy of the specified object.

Copy Constructor in Java

Copy constructor is an easy alternative to java cloning mechanism.

Copy constructor is helpful when we want to copy an object that is heavy to instantiate. While writing copy constructor it’s very important to perform deep copy so that both the objects are detached. However in some cases where you don’t mind the change in object data, then you can also go for shallow copy.

Let’s see how to properly write a copy constructor in java and perform deep copy of the object.


package com.journaldev.copyconstructor;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@ToString
public class State {

	private List<String> cities;
	private String name;
	private String country;
		
	public State(State st) {
		this.name = st.name; //string is immutable, so we can do direct assignment
		this.country = st.country;

		List<String> ct = new ArrayList<>();
		for (String c : st.cities) {
			ct.add(c);
		}
		this.cities = ct;
	}
}

Note that I am using project lombok so that I don’t have to write boiler-plate code.

Here is a simple test class where we are using copy constructor.


package com.journaldev.copyconstructor;

import java.util.ArrayList;
import java.util.List;

public class StateTest {

	public static void main(String[] args) {
		State state = getState();
		State stateCopy = new State(state);
		System.out.println("State = "+state);
		System.out.println("StateCopy = "+stateCopy);

		stateCopy.getCities().add("Cupertino");
		stateCopy.setCountry("United States of America");
		
		System.out.println("State = "+state);
		System.out.println("StateCopy = "+stateCopy);

	}

	private static State getState() {
		//in real life, it will do some DB call or expensive API
		//class to fetch the data
		State state = new State();
		state.setName("California");
		state.setCountry("USA");
		List<String> cities = new ArrayList<>();
		cities.add("San Jose"); cities.add("San Francisco");
		state.setCities(cities);
		return state;
	}

	
}

Notice that I am changing some properties in one of the object, if our deep copy implementation is correctly done then it shouldn’t affect the other object. Let’s run the above program and check the output.


State = State(cities=[San Jose, San Francisco], name=California, country=USA)
StateCopy = State(cities=[San Jose, San Francisco], name=California, country=USA)

State = State(cities=[San Jose, San Francisco], name=California, country=USA)
StateCopy = State(cities=[San Jose, San Francisco, Cupertino], name=California, country=United States of America)

It’s clear from the output that our copy constructor implementation is correct, change in one of the object didn’t affected the other object and our deep copy implementation is correct.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages