Action Details

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:

Before: 27
After: 27

Notice that the zero() method did not change the value held in the main() method's variable.

Action Details

The program: Full description of the action:
// Review Program
//
class Alteration
{
  void zero ( int x )
  {
    x = 0;
  }
}

class AlterTest
{
  public static void main ( String[] args )
  {
    Alteration alt = new Alteration();
    int value = 27;
    System.out.println( "Before:" + value );
    
    alt.zero( value );
    System.out.println( "After:"  + value );
  }
}
  1. The program starts running with the static main() method.
  2. An Alteration object is constructed.
    • The default constructor is used, since the class Alteration did not define a constructor.
    • The Alteration object contains the zero() method.
  3. The primitive int variable value is initialized to 27.
  4. The number currently held in value is printed out.
    • Before: 27 is printed
  5. The zero() method is called with value as a parameter.
    • The number held in value (27) is copied to the formal parameter x.
    • The zero() method changes x to zero.
    • Control returns to the caller; value has not been altered.
  6. The number currently held in value is printed out.
    • After: 27 is printed.

QUESTION 3:

What is the name of the parameter passing mechanism that Java uses?