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 807929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:28:52+00:00 2026-05-15T00:28:52+00:00

In Effective Java, the author states that: If a class implements Cloneable, Object’s clone

  • 0

In Effective Java, the author states that:

If a class implements Cloneable,
Object’s clone method returns a
field-by-field copy of the object;
otherwise it throws
CloneNotSupportedException.

What I’d like to know is what he means with field-by-field copy. Does it mean that if the class has X bytes in memory, it will just copy that piece of memory? If yes, then can I assume all value types of the original class will be copied to the new object?

class Point implements Cloneable{
    private int x;
    private int y;

    @Override
    public Point clone() {
        return (Point)super.clone();
    }
}

If what Object.clone() does is a field by field copy of the Point class, I’d say that I wouldn’t need to explicitly copy fields x and y, being that the code shown above will be more than enough to make a clone of the Point class. That is, the following bit of code is redundant:

@Override
public Point clone() {
    Point newObj = (Point)super.clone();
    newObj.x = this.x; //redundant
    newObj.y = this.y; //redundant
}

Am I right?

I know references of the cloned object will point automatically to where the original object’s references pointed to, I’m just not sure what happens specifically with value types. If anyone could state clearly what Object.clone()‘s algorithm specification is (in easy language) that’d be great.

  • 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-15T00:28:53+00:00Added an answer on May 15, 2026 at 12:28 am

    Yes, a field by field copy does mean that when it creates the new (cloned) object, the JVM will copy the value of every field from the original object into the cloned object. Unfortunately this does mean that you have a shallow copy. If you desire a deep copy, you can override the clone method.

    class Line implements Cloneable {
    
        private Point start;
        private Point end;
    
        public Line() {
            //Careful: This will not happen for the cloned object
            SomeGlobalRegistry.register(this);
        }
    
        @Override
        public Line clone() {
            //calling super.clone is going to create a shallow copy.
            //If we want a deep copy, we must clone or instantiate
            //the fields ourselves
            Line line = (Line)super.clone();
            //assuming Point is cloneable. Otherwise we will
            //have to instantiate and populate it's fields manually
            line.start = this.start.clone();
            line.end = this.end.clone;
            return line;
        }
    }
    

    Also one more important thing about the cloning is, the constructor of the cloned object is never invoked (only the fields are copied). So if the constructor initializes an external object, or registers this object with some registry, then that will not happen for the cloned object.

    I personally prefer to not use Java’s cloning. Instead I usually create my own “duplication” methods.

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

Sidebar

Ask A Question

Stats

  • Questions 452k
  • Answers 452k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MyFunc="SquareHandler" Means set "MyFunc" property to a "SquareHandler" string and… May 15, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer You are overreleasing something somewhere. You might want to run… May 15, 2026 at 9:03 pm
  • Editorial Team
    Editorial Team added an answer In a macro, you can use the CONDITIONS column to… May 15, 2026 at 9:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.