How are objects passed to a method in java (by value Or by reference)?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/40480/is-java-pass-by-reference">Is Java &ldquo;pass-by-reference&rdquo;?</a>  </p>
</blockquote>



<p>I want to know how to pass an object as a parameter to a method by referance in java.
I tried this code </p>

<pre><code>public class Class1 
{
    public Class1()
    {
        String x = null;
        F(x);
        //x = null
        System.out.println("x = " + x);
    }

    void F(String x)
    {
        x = "new String";
    }

    public static void main(String[] args)
    {
        new Class1();
    }
}
</code></pre>

<p>You can see that I pass a <code>String</code> to a function F and I change String's value inside it, but I can't see the changes I made on it outside the Function F. When I execute this code, I got <code>//{x = null}</code> which is not what I expected <code>//{x = new String}</code>.</p>
