Java Object clone()

In Java, an object is an instance of a class. A class is a blueprint or template for creating objects, and it defines the properties and methods of the objects created from it. An object has two main characteristics: State: the data stored in the object's fields or variables. This includes the values of the object's properties and any other information the object needs to store. Behavior: the actions the object can perform, defined by the methods of the class.

What is Object in java.

In Java, an object is an instance of a class. A class is a blueprint or template for creating objects, and it defines the properties and methods of the objects created from it.

An object has two main characteristics:

  • State: the data stored in the object’s fields or variables. This includes the values of the object’s properties and any other information the object needs to store.
  • Behavior: the actions the object can perform, defined by the methods of the class.

To know more about Object toString in java read the complete article.

Object clone() method.

The clone() method in Java is a method defined in the Object class, which is the parent class of all classes in Java. It creates and returns a copy of the object on which it is called. 

  • When the clone() method is called on a class that does not implement the Cloneable interface, a CloneNotSupportedException is thrown.
  • The clone() method creates a new object with the same state as the original object. However, it’s important to note that the clone() method is a protected method, and it’s not recommended to use it directly. It’s recommended to use the clone() method in the context of an interface such as Cloneable which enforces the class to provide a proper implementation of the clone() method.

Syntax:

Object.clone

Parameters:

The clone() method does not take any parameters.

Let’s look at a object-related Java program where the Java object clone Method is used to perform an operation on the given object.

Example: Java Object clone() Method

Run
public class Main {
  public static void main(String[] args) {

class Person implements Cloneable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}}
}

Output

Original person: John Doe, 30
Clone person: John Doe, 30

Example 2 : Java object clone() Method

Run
public class Main {
  public static void main(String[] args) {
class Car implements Cloneable {
    private String model;
    private int year;
    private double price;

    public Car(String model, int year, double price) {
        this.model = model;
        this.year = year;
        this.price = price;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}}
}

Output

Original Car: BMW, 2020, 50000.0
Clone Car: BMW, 2020, 55000.0
Original Car after changing clone: BMW, 2020, 50000.0

Example 3: how to use the object clone property:

Run
class Engine implements Cloneable {
    private int horsepower;

    public Engine(int horsepower) {
        this.horsepower = horsepower;
    }

    public int getHorsepower() {
        return horsepower;
    }

    public void setHorsepower(int horsepower) {
        this.horsepower = horsepower;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Main {
    public static void main(String[] args) {
        Engine originalEngine = new Engine(250);
        System.out.println("Original engine horsepower: " + originalEngine.getHorsepower());
        try {
            Engine clonedEngine = (Engine) originalEngine.clone();
            System.out.println("Cloned engine horsepower: " + clonedEngine.getHorsepower());
            clonedEngine.setHorsepower(300);
            System.out.println("Original engine horsepower after modifying cloned object: " + originalEngine.getHorsepower());
            System.out.println("Cloned engine horsepower after modifying: " + clonedEngine.getHorsepower());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}   

Output

Original engine horsepower: 250
Cloned engine horsepower: 250
Original engine horsepower after modifying cloned object: 250
Cloned engine horsepower after modifying: 300

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription