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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:26:55+00:00 2026-05-27T07:26:55+00:00

Sometimes i want to replace an object with another object of the same class.

  • 0

Sometimes i want to “replace” an object with another object of the same class.
Usually i do this in the following way.

The object’s class with a constructor:

public class Type {

    private int field;
    private double anotherField;

    public Type(Type anotherTypeInstance) {
        this.field = anotherTypeInstance.getField();
        this.anotherField=anotherTypeInstance.getAnotherField();
    }
}

So whenever i want to replace the object I simply do this

Type oldInstance = new Type(newInstance)

Sometimes it is easy and convenient to do, while others not.
Is there an alternative?

EDIT:

I need this kind of “replacement” in optimization algorithms.
Where i have to replace the current Solution (object) whith another Solution that has a smaller cost

  • 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-27T07:26:55+00:00Added an answer on May 27, 2026 at 7:26 am

    You can implement Cloneable interface and the clone() method like this

    @Override
    public Type clone() {
      try {
        return (Type) super.clone();
      } catch (CloneNotSupporedException cnse) {
        throw new RuntimeException(cnse); // Never happens.
      }
    }
    

    If you do use clone() for more complex classes, you need to make sure that all mutable objects referred to by fields of the class are recursively cloned as well, i.e. that you make a deep copy instead of a shallow copy. Accidental state sharing that can occur as a result of shallow copy can lead to problems which are hard to debug.

    The use of clone() has a number of problems. It circumvents constructors which are normally a place where class’s initial invariants are enforced. It is very error prone for classes containing more than just primitive types. Also, the use of clone() is incompatible with non-primitive final fields, since you cannot fix them after cloning an object in case they require modification to ensure deep copying.

    Consider for example this class:

    public class Car {
      private final Engine engine;
    
      // ... 
    
      @Override
      public Car clone() {
        try {
          Type copy = (Type) super.clone();
          copy.engine = (Engine) engine.clone(); // Error: final field cannot be modified.
          return copy;
        } catch (CloneNotSupportedException cnse) {
          throw new RuntimeException(cnse);
        }
      }
    }
    

    Copy constructors like the one posted in the question are a much better approach.

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

Sidebar

Related Questions

I have 2 classes both inherit from the same interface. i want sometimes to
If I'm not online, I sometimes want to package some changes to a commit,
Sometimes I want to build Python or GCC from scratch just for fun, but
When using powershell, sometimes I want to only display for example the name or
I have a flash program that loads movie clips dynamically and sometimes they want
As I learn new features in Oracle, sometimes I want to test how a
Windows's Snipping tool can capture the screen, but sometimes I want to capture the
sometimes when i want a winforms control to be docked also to the right
Sometimes, in PL SQL you want to add a parameter to a Package, Function
Sometimes I find myself in the situation where I want to execute several sequential

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.