Have you actually tried the first example? You may be surprised.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Because you never called the reset method from within main.
public class ArrayReference {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {1, 2, 3, 4, 5};
setArray(array);
for (Integer i: array) System.out.println(i);
}
public static void setArray(int[] array){
array[0] = 6;
array[1] = 7;
array[2] = 8;
array[3] = 9;
array[4] = 10;
array = new int[5];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;
}
}
What you have to consider is what your 'array' variable actually points to. Changing thecontents of your 'array' variable from another method will change the contents of 'array' back in main(). But changing the reference of the 'array' variable from another method will not change the reference back in main. Hence lines 20-25 in my above code have no affect on 'array' back in main because on line 20 what 'array' referred to changed, but only for the method I'm currently in.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354