best practice for modifying object reference values in java?
<p>I am fairly new to Java, and recently I was reading some material about Java being pass-by-value. I've read over <a href="http://stackoverflow.com/questions/498747/java-pass-by-value-reference-variables">this question</a>, and <a href="http://javadude.com/articles/passbyvalue.htm" rel="nofollow">this blog</a> before running a test myself.</p>

<p>Now, based on my reading and my quick test, I found that there are two ways that I can alter the variables contained within an object reference. Which of the below approaches is the better or safer approach? Are there any obvious issues with either approach?</p>

<p>Both of these print out "iArr[0] = 45".</p>

<p><strong>Approach 1:</strong></p>

<pre><code>public static void main(String args[] ){
   int[] iArr = {1};
   method(iArr) ;
   System.out.println( "iArr[0] = " + iArr [0] ) ;
}
public static void method(int[] n ) { 
    n [0] = 45 ;
}
</code></pre>

<p><strong>Approach 2:</strong></p>

<pre><code>public static void main(String args[] )
{
   int[] iArr = {1};
   iArr = method(iArr) ;
   System.out.println( "iArr[0] = " + iArr [0] ) ;
}
public static int[] method(int[] n ) { 
    n [0] = 45 ;
    return n;
}
</code></pre>
