When the method is called, ar1
is a reference to the array object.
The method findMax()
gets a reference to the array, so it can
access the elements of that array object.
Here is a complete program that contains both classes.
class ArrayOps { // the parameter x refers to the data int findMax( int[] x ) // this method is called with. { int max = x[0]; for ( int index=0; index <x.length; index++ ) if ( x[index] > max ) max = x[index] ; return max ; } } class ArrayDemo { public static void main ( String[] args ) { int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ; ArrayOps operate = new ArrayOps(); // create an ArrayOps object int biggest = operate.findMax( ar1 ); // call the findMax() method with the array System.out.println("The maximum is: " + biggest ); } }
When you run the program it prints out "The maximum is: 27". You might want to copy this program to a file (call it ArrayDemo.java) and play with it.
During one run of the program, how many arrays are created?