


Java Clone Brief Review: Definition, Code, Pros/Cons & Types
Jun 27, 2025 am 12:50 AMThis article provided by MiniTool official gives a concise overview of the Java clone and explains its definition, approach, advantages, drawbacks, and types. Go ahead and read it thoroughly to gain deeper insights into the clone () method in the JAVA language.
What Is Java Clone?
Java clone, clone(), pertains to the clone method in Java programming language for creating a duplicate of an object. In Java, manipulation of an object happens via reference variables. There is no operator designed for duplicating objects. The assignment operator merely copies the reference rather than the object itself. Hence, the Java clone method serves as the duplication feature of the object.
Regarding the Java Clone() Method
Classes aiming to replicate functionality must incorporate certain methods to accomplish this. To some degree, such functionality is provided by Object.clone(). Clone () operates like a copy constructor. Generally, it invokes the clone method of its superclass to obtain the copy, continuing until it ultimately reaches the Object.cloen() method.
The specialized clone method within the base class Object presents a standardized approach for duplicating objects. The class object’s clone method generates and returns a replica of the object sharing the same class along with all field values being identical.
However, Object.clone() throws a CloneNotSuppertedException unless the object is an instance of a class implementing the marker interface Clonable. The default implementation of Object.clone() performs a shallow copy. If a class desires a deep copy or other custom behavior, they must implement this within their own Java clone method after obtaining the copy from the superclass.
The syntax to call clone in Java is as follows:
Object copy = obj.clone();
OR
MyClass copy = (MyClass) obj.clone();
Note: The “obj” is a variable of a class type possessing a public clone method.
The latter clone command supplies the necessary typecasting to assign the general object reference returned from clone to a reference of a MyClass object.
Java Clone Alternatives
Generally, there exist 2 java clone alternatives: the copy constructor and the factory method. The copy constructor is a constructor that takes as a parameter another instance of the same class.
Both alternatives aren't always sufficient when the concrete type of the Java clone object isn't predetermined. Similarly, the clone method isn't often suitable either due to the same reason that most abstract classes don't offer a public clone method.
Java Clone Pros and Cons
Compared to the copy constructor and assignment operator, the clone method in Java boasts both benefits and limitations.
Benefits of Java Clone
- When utilizing the copy constructor, you must manually copy all data. Conversely, with the clone method, creating a new copy is handled by the method itself without requiring additional steps. Consequently, cloning requires fewer lines of code.
- Cloning represents the simplest method to duplicate objects, particularly beneficial for existing or older projects.
- Clone is also the fastest approach to copy arrays.
- Relying on the assignment operator to assign an object reference to another reference variable results in both pointing to the same memory address of the original object without generating a new copy. Thus, modifications made through the reference variable will reflect in the original object, even if unintended.
Drawbacks of Java Clone
Despite the utility of Java cloning and its ability to directly duplicate an object, it does possess certain disadvantages.
Firstly, the return type of clone is the object. Therefore, the return type must be explicitly cast back to the correct type. Since J2SE 5.0, leveraging covariant return types to override clone and return the appropriate type is preferred, eliminating the need for client-side casting.
Secondly, you cannot access the clone method on an abstract type. Most abstract classes and interfaces in Java do not specify a public clone method. As a result, typically, the clone method can only be accessed if the actual class of an object is known, contradicting the abstraction principle of using the most generic type possible.
For example, if you have a List reference in Java, you cannot invoke the clone method on that reference because List specifies no public clone method. Actual implementations of List, such as ArrayList and Linked List, almost all have clone methods. However, this constitutes poor abstraction and is inconvenient when dealing with the actual class type of an object.
There are several other disadvantages as well:
- You cannot control object construction since clone() doesn't invoke any constructor.
- You cannot modify final fields within clone() because final fields can only be altered by constructors.
- clone() is protected, necessitating the creation of your own clone method and indirectly calling Object.clone() from it.
- clone() only supports shallow cloning. Thus, the reference fields of your newly cloned object still retain objects that were referenced by the fields of your original object.
- If writing a clone method into a child class, all of its superclasses should define the clone method in them or inherit the clone method from other parent classes.
- Using the clone method involves adding considerable syntax to your code, including calling clone() and casting it on your object, defining the clone method, implementing a cloneable interface, and handling CloneNotSuppertedException.
Shallow Clone vs Deep Clone
Shallow copy is a method of duplicating an object, following the default behavior in the JAVA clone. Shallow copy is straightforward and inexpensive. On the other hand, a deep copy duplicates all fields, creating copies of dynamically allocated memory referenced by the fields. A deep Java clone occurs when an object is copied along with the objects it references.
You May Also Like
- A Quick Fix Guide to Application Blocked by Java Security Issue
- [Brief Review] How to Clone a Phone of Its Identity and Data
- [Tutorial] How to Use GIMP Clone Tool to Clone Image/Pattern
The above is the detailed content of Java Clone Brief Review: Definition, Code, Pros/Cons & Types. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

This Google translate picture guide shows you how to translate text from an image. If you are looking for more computer tips and solutions, you can visit php.cn Software official website where you can also find some useful computer tools like php.cn

The operating system is the basic software for managing hardware resources, running programs, and providing user interaction interfaces. It coordinates the relationship between hardware and software and is responsible for memory allocation, device scheduling, file management and multitasking. Common systems include Windows (suitable for office and gaming), macOS (Apple devices, suitable for creative work), Linux (open source, suitable for developers), and Android/iOS (mobile device system). The choice of ordinary users depends on the usage scenario, such as software compatibility, security and customization requirements. How to view system information: Use winver command for Windows, click on the machine for macOS, use terminal commands for Linux, and find the phone in settings. The operating system is the underlying tool for daily use,

Have you ever wanted to adjust computer settings to fix some issues but suffered from Control Panel not opening? There is nothing more frustrating than this app not turning on, stopping you from viewing and changing system settings. In this post, mul

What is Dell Digital Locker? How to log into Dell Digital Locker? This post from php.cn provides answers. Besides, you can know how to use your Dell Digital Locker to find software products included with your Dell computer.

This essay summarized by php.cn Software mainly teaches you how to open Windows 11 Computer Management with Windows Search, Quick Link menu, Run dialog, command prompt, PowerShell, File Explorer, Control Panel, as well as a desktop shortcut.

AMD offers an AMD Driver Auto-Detect Tool to help users automatically download and update the drivers of the installed AMD products. Check how to download AMD Driver Auto-detect Tool on Windows 10/11 and how to use it to get the latest AMD drivers. F

What is Airplane Mode on iPhone? What does Airplane Mode do on iPhone? How to turn it on your iPhone? When to use it? This post from php.cn introduces information about iPhone Airplane Mode.

People usually access restricted websites that are blocked in their region via VPN. But what if you encounter the VPN not working on Chrome? That can really drive you crazy. Fortunately, you can find a couple of useful solutions in this guide. Get st
