title: Pass by Value and reference Java
body: <p>I am trying to write a copy method which can be used to copy a bean to bean by mapping the property names. if the source object is a cloneable object m cloning it and trying to return it to the calling method. but in calling method it change made in the called method is not reflecting </p>
  
  <p>you might understand if u look at the below code</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
  
          HashMap&lt;Object, Object&gt; source = new HashMap&lt;Object, Object&gt;();
          source.put("test1", "test1");
          source.put("test2", "test2");
          source.put("test3", "test3");
  
          HashMap&lt;Object, Object&gt; destination = new HashMap&lt;Object, Object&gt;();
          Main m = new Main();
  
          Main.beanCopy(source, destination);
          System.out.println("caller - " + destination);
  
  
      }
  
      public static void beanCopy(Object source, Object destination) {
          if (source.getClass() == destination.getClass()
                  &amp;&amp; Cloneable.class.isInstance(source)) {
              Class&lt;? extends Object&gt; cl = source.getClass();
              try {
                  Method method = cl.getDeclaredMethod("clone");
                  destination = method.invoke(source);
                  System.out.println("callee - " +  destination);             
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  
  <p>the output of this program is </p>
  
  <pre><code>callee - {test1=test1, test2=test2, test3=test3}
  caller - {}
  </code></pre>
  
  <p>I hope someone could help me to resolve this problem..
  Thanks in advance...</p>