title: Java : Issue with Object Cloning?
body: <p>I have <code>invoice</code> object and it has two properties, <code>productDescription</code> and <code>productQuantity</code>, here is the code:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class Invoice() implements Cloneable
  {
    private String productDescription;
    private int productQuantity;
  
    //Getters &amp; Setters
  
    public Object clone() throws CloneNotSupportedException {
       return super.clone();
    }
  }
  </code></pre>
  
  <p>Now i want to create copy of object on this class so am doing </p>
  
  <pre class="lang-java prettyprint-override"><code>Invoice invoiceCopy = (Invoice)invoice.clone();
  </code></pre>
  
  <p>Now if i update value of <code>invoiceCopy.setProductQuanity = 10</code> then <code>invoice</code> also updates the value but that should not happen, any suggestions?</p>
  
  <h2><strong>Updated Code</strong></h2>
  
  <pre class="lang-java prettyprint-override"><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());
      }
  }
  </code></pre>