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. If you are going to do that, read at least what Joshua Bloch has to say about it in Effective Java.

Answer from egaga on Stack Overflow
Top answer
1 of 16
713

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. If you are going to do that, read at least what Joshua Bloch has to say about it in Effective Java.

2 of 16
460

Basic: Object Copying in Java.

Let us Assume an object- obj1, that contains two objects, containedObj1 and containedObj2.

shallow copying:
shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it. Object class provides a clone method and provides support for the shallow copying.

Deep copying:
A deep copy occurs when an object is copied along with the objects to which it refers. Below image shows obj1 after a deep copy has been performed on it. Not only has obj1 been copied, but the objects contained within it have been copied as well. We can use Java Object Serialization to make a deep copy. Unfortunately, this approach has some problems too(detailed examples).

Possible Problems:
clone is tricky to implement correctly.
It's better to use Defensive copying, copy constructors(as @egaga reply) or static factory methods.

  1. If you have an object, that you know has a public clone() method, but you don’t know the type of the object at compile time, then you have problem. Java has an interface called Cloneable. In practice, we should implement this interface if we want to make an object Cloneable. Object.clone is protected, so we must override it with a public method in order for it to be accessible.
  2. Another problem arises when we try deep copying of a complex object. Assume that the clone() method of all member object variables also does deep copy, this is too risky of an assumption. You must control the code in all classes.

For example org.apache.commons.lang.SerializationUtils will have method for Deep clone using serialization(Source). If we need to clone Bean then there are couple of utility methods in org.apache.commons.beanutils (Source).

  • cloneBean will Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.
  • copyProperties will Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
🌐
Reddit
reddit.com › r/javahelp › java copy method
r/javahelp on Reddit: Java copy method
October 28, 2021 -

Hello there! I need some kind an advice. I'm trying to find out some library for Java providing possibility to copy object with changed values. For example, in Scala we have method copy() in case classes copying all fields to another one. Also there is an approach to define which fields will be changed by passing them into copy(). Does something like this exist in Java world?

Discussions

[Java] Copy constructors?
IIRC, Josh Bloch recommends not using clone, but I don't remember why. It's in his Effective Java, which I don't have with me. Just define a constructor that takes your type as its argument. Use this to call another constructor within a constructor. There are two kinds of this. There's this for instance variables. That's the one that everybody learns about. And there's this for calling one constructor from another. That's the this that people tend not to learn about. public class reddit { private int foo = 0; // default value private int bar = 0; // default value /* default (no-arg) constructor */ public reddit() { // members automatically get their default values } /* regular constructor */ public reddit(int foo, int bar) { this.foo = foo; this.bar = bar; } /* copy constructor */ public reddit(reddit r) { // call default constructor this(); // deep copy each member, although not necessary for ints foo = new Integer(r.foo); bar = new Integer(r.bar); } @Override public boolean equals(Object other) { if (other == this) return true; reddit o = (reddit) other; return foo == o.foo && bar == o.bar; } public static void main(String[] args) { reddit r = new reddit(1, 2); reddit rcopy = new reddit(r); reddit rdefault = new reddit(); System.out.println(r); System.out.println(rcopy); System.out.println(rdefault); System.out.println(r.equals(rcopy)); System.out.println(r == rcopy); System.out.println(r.equals(rdefault)); System.out.println(r == rdefault); } } More on reddit.com
🌐 r/learnprogramming
8
1
September 11, 2014
Copying files... best method?
Well I am not sure in Java but if you are using Windows already, just use robocopy? More on reddit.com
🌐 r/java
12
3
August 15, 2011
Reddit clone in one hour and 100 lines of Java

Very nice, now do it in C++

More on reddit.com
🌐 r/programming
20
0
February 7, 2010
Cloning objects in Java - why Object.clone does not work, and what to do about it
I'm no Java expert but I have implemented proper cloning in other languages. In my experience, the way it should work is a) The base class needs a public method (Duplicate(), say) that knows how to create a new "empty" object with the same class as whatever called it. (Any real OO language can do this) That public method should never be overridden. After it has done that, it just called a protected virtual method called CloneFields() b) That class should also define a protected method called CloneFields() (say) which every subclass that contains any fields would override. Each subclass's overridden version simply calls its super and then implements whatever is needed to copy its own local fields. It's certainly trivial in Smalltalk, Delphi, or Ruby. More on reddit.com
🌐 r/programming
17
0
August 23, 2008
🌐
Baeldung
baeldung.com › home › java › core java › how to make a deep copy of an object in java
How to Make a Deep Copy of an Object in Java | Baeldung
March 26, 2025 - Learn four ways to create a deep copy of an object in Java, and why to prefer a deep copy over a shallow copy.
🌐
GeeksforGeeks
geeksforgeeks.org › java › deep-shallow-lazy-copy-java-examples
Deep, Shallow and Lazy Copy with Java Examples - GeeksforGeeks
January 23, 2026 - Changes made through one object are reflected in all copies sharing the reference. ... import java.util.Arrays; class Ex { private int[] data; // Shallow copy public Ex(int[] values) { this.data = values; } public void showData() { System.out.println(Arrays.toString(data)); } }
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
Shallow and deep copy: Two ways to copy objects in Java | InfoWorld
January 4, 2024 - See my InfoWorld article, Does Java pass by reference or pass by value? for a deeper exploration of object references. The shallow copy technique allows us to copy simple object values to a new object without including the internal object values.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › copy.html
Copying a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
See Java Language Changes for a ... removed or deprecated options for all JDK releases. You can copy a file or directory by using the copy(Path, Path, CopyOption...) method....
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Clone_(Java_method)
clone (Java method) - Wikipedia
March 14, 2026 - When a class desires a deep copy or some other custom behavior, they must implement that in their own clone() method after they obtain the copy from the superclass. The syntax for calling clone in Java is (assuming obj is a variable of a class type that has a public clone() method):
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › examples › Copy.java
Java Copy example
CopyOption[] options = (preserve) ? new CopyOption[] { COPY_ATTRIBUTES } : new CopyOption[0]; Path newdir = target.resolve(source.relativize(dir)); try { Files.copy(dir, newdir, options); } catch (FileAlreadyExistsException x) { // ignore } catch (IOException x) { System.err.format("Unable to create: %s: %s%n", newdir, x); return SKIP_SUBTREE; } return CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { copyFile(file, target.resolve(source.relativize(file)), prompt, preserve); return CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path
🌐
Medium
medium.com › @dbudim › java-object-copying-shallow-vs-deep-explained-1fbed64583a6
Java Object Copying: Shallow vs Deep Explained | by Dmytro Budym | Medium
March 16, 2023 - A deep copy constructs a completely independent copy of an object, while a shallow copy makes a copy that depends on the original object. ... To support cloning in Java, a class must implement the Cloneable interface and override the clone() method.
🌐
Medium
medium.com › @avinashsingh1152 › understanding-deep-copy-in-java-exploring-pass-by-value-pass-by-reference-and-deep-copy-dba5044032cc
Understanding Deep Copy in Java: Exploring Pass by Value, Pass by Reference, and Deep Copy Techniques | by Avinash Kumar Singh | Medium
February 21, 2024 - Pass by Value: Pass by value is a mechanism used in function calls where a copy of the actual data is passed to the method or function. In Java, primitive data types are passed by value, meaning that modifications made to the parameter inside the method do not affect the original value outside the method.
🌐
WJHL
wjhl.com › news › local › photos-jeeps-java-brings-community-together-saturday-morning
PHOTOS: Jeeps & Java brings community together Saturday morning | WJHL | Tri-Cities News & Weather
March 22, 2026 - The community event, ‘Jeeps & Java’ hosted by Fountain of Life Bible Church, returned early Saturday morning, for those who have a Jeep or just wanted to hang around classic cars from around the area.
🌐
Curl Converter
curlconverter.com
Convert curl commands to code
Utility for converting cURL commands to code
🌐
GitConnected
levelup.gitconnected.com › do-we-understand-the-core-of-javas-copy-concepts-c2ed32fd83b2
Java Copy Concepts - Shallow Copy vs Deep Copy | Level Up Coding
July 21, 2024 - Shallow copy creates references to existing data, which improves memory and performance. Deep copy offers total data isolation, ensuring that changes in one object don’t affect others. In this article, we’ll explore the concepts of Java’s shallow copy and deep copy and understand their significance.
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_copy_an_object_in_java
How do I copy an object in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Medium
jinwonp7.medium.com › how-to-copy-objects-in-java-2b3ce76b8777
How to copy objects in Java. In Java, there is no simple operator to… | by Jinwon Park | Medium
November 21, 2021 - How to copy objects in Java In Java, there is no simple operator to create a copy of an object. Here is an example of copying an object. @Data public class User { String name; int age; …
🌐
OneCompiler
onecompiler.com › java
Java Online Compiler
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running on Java 25. Getting started with the OneCompiler's Java editor is easy and fast.
🌐
LabEx
labex.io › tutorials › java-how-to-implement-java-object-duplication-418401
How to implement Java object duplication | LabEx
Object cloning is a process of creating an exact copy of an existing object in Java. It allows you to duplicate an object's state and create a new instance with the same properties and values.
🌐
JetBrains
blog.jetbrains.com › home › intellij idea › java 26 in intellij idea
Java 26 in IntelliJ IDEA - The JetBrains Blog
April 1, 2026 - Java 26 was released on March 17, 2026. At JetBrains, we are committed to supporting the latest technologies in IntelliJ IDEA and adding useful enhancements for both stable and preview features. In this blog post, we will give you an overview of what Java 26 delivers and how it is supported ...
🌐
GUID Generator
guidgenerator.com
Free Online GUID Generator
Free Online GUID / UUID Generator · GUID (aka UUID) is an acronym for 'Globally Unique Identifier' (or 'Universally Unique Identifier'). It is a 128-bit integer number used to identify resources. The term GUID is generally used by developers working with Microsoft technologies, while UUID ...