Still Call by Value

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:

x = 3; y = 5
Enter PointDoubler
x = 3; y = 5
x = 6; y = 10
Leave PointDoubler
x = 6; y = 10

Still Call by Value

Here are some facts:

  • Paramater passing is always call by value
  • .
  • If a method's parameter is a primitive data type, the method can change its own value for the parameter, but the change has no effect elsewhere.
  • However, if a method's parameter is a reference to an object, the instance variables of the object can be changed (if they are public).

These facts are consistent with call by value. The "value" is a reference to the object. The invoked method has its local copy of this reference and can't change the value of the reference held by the caller. However, the invoked method can change the object.

Of course, even if a method has a reference to an object, the object can be changed only if the object allows changes to be made (either with public instance variables or through access methods).


QUESTION 12:

Look at the definition of the MyPoint class. Think of a way to make MyPoint objects immutable.