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

  • Home
  • SEARCH
  • 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 692857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:41:38+00:00 2026-05-14T02:41:38+00:00

The Object.clone() method in Java is pretty special, as instead of returning a copy

  • 0

The Object.clone() method in Java is pretty special, as instead of returning a copy of the object that is to be cloned with the Object type, it returns the correct Object type. This can be better described with the following code:

class A implements Cloneable
{
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class B extends A {
}

public class MainABC {
    public static void main(String[] args) throws CloneNotSupportedException {
        B b = new B();
        B b1 = (B)b.clone(); //see here that we are using A's .clone(). The only
                             //thing it does is call Object's clone().
        System.out.println(b1.getClass()); //but as we see here, its not an Object
                                           //its a B!
    }
}

So, could anyone explain if possible if is there anyway to replicate what happens inside Object.clone()’s method?

  • 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-14T02:41:39+00:00Added an answer on May 14, 2026 at 2:41 am

    It is definitely true that Object.clone() does a few things that simply can not be achieved in Java.

    From Josh Bloch on Design: Copy Constructor versus Cloning (emphasis mine):

    Object’s clone method is very tricky. It’s based on field copies, and it’s “extra-linguistic.” It creates an object without calling a constructor. There are no guarantees that it preserves the invariants established by the constructors.

    Object.clone() does something that isn’t supposed to be allowed by the language. That is why, among many other reasons, clone() is broken.

    (If you haven’t already, you should also read his book Effective Java, to understand why he (and many others) think that Java’s clone() and Cloneable is broken).


    If you just want to create an object of the same class as another arbitrary object, then this is actually quite achievable, with some caveat (namely that not all types are publicly instantiable) by using reflection.

    Here’s an example of how to use reflection to:

    • Find out the class of an object at run-time
    • List its declared fields, methods, and constructors
    • Find its copy constructor (if any), and tries to invoke it using the given object as
      parameter.

    import java.lang.reflect.*;
    
    public class NewInstance {
       static void print(String label, Object[] arr) {
          System.out.println(label);
          for (Object o : arr) {
             System.out.println(o);
          }
          System.out.println("---");
       }
    
       static Object newInstance(Object o) {
          Class<?> c = o.getClass();
          System.out.println("Class is " + c);
          print("FIELDS:", c.getDeclaredFields());
          print("METHODS:", c.getDeclaredMethods());
          print("CONSTRUCTORS:", c.getDeclaredConstructors());
    
          try {
             Constructor<?> cc = c.getDeclaredConstructor(c);
             o = cc.newInstance(o);
          } catch (NoSuchMethodException e) {
             System.out.println("No copy constructor found!");
          } catch (IllegalAccessException e) {
             System.out.println("Copy constructor inaccessible!");
          } catch (InstantiationException e) {
             System.out.println("Instantiation failed!");
          } catch (InvocationTargetException e) {
             System.out.println("Copy constructor threw " + e.getCause());
          }
          return o;
       }
    
       public static void main(String args[]) {
          Object o1 = "hello";
          Object o2 = newInstance(o1);
          boolean success = (o1 != o2) && (o1.equals(o2));
          System.out.println("Attempt " + (success ? "succeeded!" : "failed :("));
       }
    }
    

    Output:

    Class is class java.lang.String
    FIELDS:
    // (omitted)
    METHODS:
    // (omitted)
    CONSTRUCTORS:
    public java.lang.String()
    public java.lang.String(java.lang.String) // this is what we're looking for!
    // (rest omitted)
    ---
    Attempt succeeded!
    

    Note that this is just an example to show type can be inspected at run time and a copy constructor can be looked for and invoked. As is, it doesn’t work if o is an ArrayList, because it has no constructor that takes an ArrayList (it does have one that takes a Collection, which an ArrayList is).

    I’ll leave it to you as an exercise on how to expand the search for the copy constructor to include these compatible overloads.

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

Sidebar

Related Questions

Is clone() in java a shallow copy? Eventually this gets to the clone() method
In Effective Java, the author states that: If a class implements Cloneable, Object's clone
I have an object not written by myself that I need to clone in
I've got a pretty special setup: I create all the classes in Java, connect
I have an object class called BOM. It has a method, getChildItem() which returns
I have a method that tries to clone an instance of a class (B)
I'm trying to clone the style object of an element. This should allow me
Anybody have a good example how to deep clone a WPF object, preserving databindings?
Is there a generic way to clone objects in VBA? So that i could
Java doc says - The class Object does not itself implement the interface Cloneable,

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.