Method Changing an Element

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes.

Method Changing an Element

parameter passing

 

There is only one array, and the method has a reference to it. The method is able to change the elements of the array, as the picture shows.

The difference between the first program and this example program is that arrays are objects. When a method is called with an object reference (like value), the method gets a reference to the object and can change the object (if the object allows changes.)

Some objects do not allow any changes, such as String objects. So a method can't change them even though it has a direct reference to the object.

QUESTION 8:

If main() had the following method call, what would happen to the array?

cng.zeroElt( value, 4 );