Object References as Parameters

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:

First  value of the result: 0
Second value of the result: 14

Object References as Parameters

Object references can be parameters. Call by value is used, but now the value is an object reference. This reference can be used to access the object and possibly change it. Here is an example program:

class ObjectPrinter
{
  public void print( String st )
  {
    System.out.println("Value of parameter: " + st );    
  }
}

class OPTester
{
  public static void main ( String[] args )
  {
    String message = "Only One Object" ;

    ObjectPrinter op = new ObjectPrinter();

    System.out.println("First  value of message: " + message );    
    op.print( message );
    System.out.println("Second value of message: " + message );    
  }
}

In this program, the local variable message contains a reference to a String object. A copy of that reference is made in the formal parameter st of the print() method. The object is not copied.

QUESTION 6:

What is the output of the program?

First  value of message: 
Value of parameter: 
Second value of message: