Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6103731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:45:44+00:00 2026-05-23T13:45:44+00:00

I have a simple program to clone a object , I googled the error

  • 0

I have a simple program to clone a object , I googled the error “Exception in thread “main” java.lang.CloneNotSupportedException:” but need your help to understand the error, why am I not able to get clone of obj1?

public class Test{
int a;
int b;
public Test(int a , int b){
    this.a=a;
    this.b=b;
    }
public static void main(String[]args) throws CloneNotSupportedException{
    Test obj1=new Test(2, 4);
    Test obj2=(Test) obj1.clone();
       }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:45:45+00:00Added an answer on May 23, 2026 at 1:45 pm

    The problem occurs because the class Test doesn’t implement the Cloneable interface. As stated in the API specs,

    if the class [..] does not implement the interface Cloneable, then a CloneNotSupportedException is thrown.

    To fix, try something like:

    public class Test implements Cloneable { 
      ...
    }
    

    Since the Cloneable interface declares no methods (it’s called a marker interface, just like Serializable), there’s nothing more to do. The instances of your Test class can now be cloned.

    However, the default cloning mechanism (ie, that of Object) might not be exactly what you are looking for, and you might want to override the clone() method. The default is to make a shallow copy, that is, you will get a new, distinct instance of your class, but the fields of both instances will refer to the same objects! For example:

    class C1 {
      Object o;
    }
    class C2 implements Cloneable {
      C1 c1;
    }
    
    ... main ... {
      C2 c2 = new C2();
      c2.c1 = new C1();
      c2.c1.o = new Object();
      C2 c2clone = c2.clone();
      System.out.println(c2 == c2clone); // prints false
      System.out.println(c2.c1 == c2clone.c1); // prints true
      c2.c1.o = new Object(); // modified both c2 and c2clone!!!
    

    The last line will modify both c2 and c2clone because both point to the same instance of c1. If you want the last line to modify only c2, then you have to make what we call a deep copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple java program that runs fine in eclipse but cannot find
I have a simple program that creates a thread, loops twenty times and then
I have a simple program to check if a port is open, but I
Possible Duplicate: Java: how to clone ArrayList but also clone its items? I have
I have a simple Java program (using JTOpen) to connect to an iSeries and
I have a simple Java program that reads in a text file, splits it
Say I have simple program that emulates a board game with a number of
I have a simple python program that I'd like to daemonize. Since the point
I have a simple forms program that I have been fighting with for a
I have a simple Delphi program that I'm working on, in which I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.