Answer:
Just one, created in main().
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().findMax()x.
Here are some details:
ArrayDemois shown in dotted lines because it is a class definition, not an object.ArrayDemo's static methodmain()is running.- The variable
ar1refers to the array object. - The variable
operaterefers to anArrayOpsobject. - The picture shows what happens when the
method offindMax()operateis called withar1as a parameter. - Just after the call, the formal parameter
xrefers to the same object as does the variablear1inmain().
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?