go to previous page   go to home page   go to next page

Answer:

Just one, created in main().

Call by Value

call by value

The picture shows the executing program just as the findMax() method starts running. Remember that Java uses call by value to pass data into methods. With array parameters, this means that the parameter holds the value of the reference to the array.

There is only one array object, the one created by main(). When the findMax() method is running it has a reference to this array in its parameter x. Here are some details:

  1. ArrayDemo is shown in dotted lines because it is a class definition, not an object.
  2. ArrayDemo's static method main() is running.
  3. The variable ar1 refers to the array object.
  4. The variable operate refers to an ArrayOps object.
  5. The picture shows what happens when the findMax() method of operate is called with ar1 as a parameter.
  6. Just after the call, the formal parameter x refers to the same object as does the variable ar1 in main().

Now the findMax() method runs, using x to refer to the array it is working with. When it finishes running, control returns to main().

Usually you do not think about things in such excruciating detail. You would think "call a method to find the maximum" and would write

operate.findMax( ar1 ); 

But sometimes you really need to know what is going on. Please look the picture over and read over the details a few times. If you rush through this material, you risk missing concepts that are necessary for understanding future topics.

QUESTION 6:

(Test of Understanding: ) Could the main() method create a second array and use the findMax() method with it?