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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:50:52+00:00 2026-06-09T07:50:52+00:00

I have invoice object and it has two properties, productDescription and productQuantity , here

  • 0

I have invoice object and it has two properties, productDescription and productQuantity, here is the code:

public class Invoice() implements Cloneable
{
  private String productDescription;
  private int productQuantity;

  //Getters & Setters

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

Now i want to create copy of object on this class so am doing

Invoice invoiceCopy = (Invoice)invoice.clone();

Now if i update value of invoiceCopy.setProductQuanity = 10 then invoice also updates the value but that should not happen, any suggestions?

Updated Code

public class Invoice implements Cloneable {
    private String productDescritpion;
    private int productQuantity;

    public String getProductDescritpion() {
        return productDescritpion;
    }

    public void setProductDescritpion(String productDescritpion) {
        this.productDescritpion = productDescritpion;
    }

    public int getProductQuantity() {
        return productQuantity;
    }

    public void setProductQuantity(int i) {
        this.productQuantity = i;
    }

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

    public static void main(String[] args) {
        Invoice invoice = new Invoice();
        invoice.setProductQuantity(10);
        invoice.setProductDescritpion("nike");
        Invoice invoiceCopy = null;
        try {
            invoiceCopy = (Invoice) invoice.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        invoiceCopy.setProductQuantity(20);
        System.out.println("Invoice quantity: " + invoice.getProductQuantity());
        System.out.println("InvoiceCopy quantity: "
                + invoiceCopy.getProductQuantity());
    }
}
  • 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-06-09T07:50:53+00:00Added an answer on June 9, 2026 at 7:50 am

    default clone methode is a superficial one.
    you have to write yourself a new method or use a library that does what you want (see apache’s BeanUtils for exemple)

    if you want to write it, try something like :

    public class Invoice implements Cloneable {
    private String productDescription;
    private int productQuantity;
    
    public Invoice() {
    }
    
    public Invoice(Invoice copy) {
        productDescription = copy.productDescription;
        productQuantity = copy.productQuantity;
    }
    
    public Object clone() throws CloneNotSupportedException {
        return new Invoice(this);
    }
    

    }

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

Sidebar

Related Questions

So, I have an Invoice object like so: public class Invoice { public virtual
I have an object Invoice which belongs to an Area: public class Invoice :
I have an object JobBreakdown that has_one :invoice . If a JobBreakdown has an
So I have two objects, Invoice and InvoiceLineItem. InvoiceLineItem has a property called cost
I have two entities: Invoice and InvoiceDetail. Invoice has an InvoiceDetails member. When I
Take this POJO as the domain. public class Invoice { private String codeNumber; private
Assume we have an Invoice entity which has many InvoiceDetails . Each invoice detail
I have a basic invoice setup with models: Invoice, Item, LineItems. # invoice.rb class
I have two classes Invoice and InvoiceItem. I would like Invoice to have a
I have two tables: Invoice (ID, InternalPrice, ExternalPrice) InvoiceSummary (InvoiceID, Variance ) In this

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.