title: What happens when an object is assigned to another object
body: <pre><code>public class DrumKitTestDrive {
  /**
   * @param args
   */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      Echo e1 = new Echo();
      Echo e2 = new Echo();
  //      **e2 = e1;**
  
      int x=0;
      while( x &lt; 4 ){
          e1.hello();
          e1.count = e1.count + 1;
          if(x==3){
              e2.count = e2.count + 1;
          }
          if(x&gt;0){
              e2.count = e2.count + e1.count;
          }
          x = x + 1;
      }
      System.out.print(e2.count);
      }
  }
  
  class Echo {
      int  count = 0;
  
      void hello(){
          System.out.println("Hellooooo....");
      }
  }
  </code></pre>
  
  <p>The output of this code will be:</p>
  
  <pre><code>Hellooooo....
  Hellooooo....
  Hellooooo....
  Hellooooo....
  10
  </code></pre>
  
  <p>But if I remove the comments from <code>// e2= e1;</code> when I run the code the system will print <em>24</em> instead of <em>10</em>. I don't understand why is it so?</p>
  
  <p>What I understand is the system will just copy the values of <code>e1</code> instance into <code>e2</code> instance. And if the system will do so, the result shall remain the same because both objects are of the same class.</p>