Chapter 5: References Revisited

References revisited:

– An object reference variable and an object are two different things.
– The declaration of the reference variable and the creation of the object that it refers to are separate steps.
– The dot operator uses the address in the reference variable.
– A reference variable that doesn’t point to an object is called a null reference.
– When a reference variable is first declared as an instance variable, it is a null reference.
– Accessing a null reference will cause a NullPointerException.

What is the problem with this class?

1. public class ACommonMistake
2.{
3.  public ACommonMistake()
4.  {
5.    System.out.println(lastName.length());
6.  }

7.  public void aMethod()
8.   {
9.    String firstName;
10.   System.out.println(firstName.length());
11.  }

12. private String lastName;
13.}

The this reference:
– The “this” word is a reserved word in Java.
– It lets an object refer to itself.
– A method is always invoked through (or by) a particular object or class.
– Inside the method, the “this” reference can be used to refer the currently executing object.

Aliases:
An object reference variable stores an address therefore an assignment like this:

ChessPiece bishop1 = new ChessPiece();
ChessPiece bishop2 = bishop1;
Will make the two object variables refer to the same object.

Method Parameters Revisited:

In Java, all parameters are passed by value. That is, the current value of the actual parameter (in the invocation) is copied into the formal parameter in the method header.

//********************************************************************
//  ParameterTester.java       Author: Lewis/Loftus
//
//  Demonstrates the effects of passing various types of parameters.
//********************************************************************

public class ParameterTester
{
   //-----------------------------------------------------------------
   //  Sets up three variables (one primitive and two objects) to
   //  serve as actual parameters to the changeValues method. Prints
   //  their values before and after calling the method.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      ParameterModifier modifier = new ParameterModifier();

      int a1 = 111;
      Num a2 = new Num (222);
      Num a3 = new Num (333);

      System.out.println ("Before calling changeValues:");
      System.out.println ("a1\ta2\ta3");                       
      System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n");   // 1st print

      modifier.changeValues (a1, a2, a3);

      System.out.println ("After calling changeValues:");
      System.out.println ("a1\ta2\ta3");
      System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n");  // 4th print
   }
}
//********************************************************************
//  ParameterModifier.java       Author: Lewis/Loftus
//
//  Demonstrates the effects of changing parameter values.
//********************************************************************

public class ParameterModifier
{
   //-----------------------------------------------------------------
   //  Modifies the parameters, printing their values before and
   //  after making the changes.
   //-----------------------------------------------------------------
   public void changeValues (int f1, Num f2, Num f3)
   {
      System.out.println ("Before changing the values:");
      System.out.println ("f1\tf2\tf3");
      System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n");  // 2nd print

      f1 = 999;
      f2.setValue(888);
      f3 = new Num (777);

      System.out.println ("After changing the values:");
      System.out.println ("f1\tf2\tf3");
      System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n");  // 3rd print
   }
}

//********************************************************************
//  Num.java       Author: Lewis/Loftus
//
//  Represents a single integer as an object.
//********************************************************************

public class Num
{
   private int value;

   //-----------------------------------------------------------------
   //  Sets up the new Num object, storing an initial value.
   //-----------------------------------------------------------------
   public Num (int update)
   {
      value = update;
   }

   //-----------------------------------------------------------------
   //  Sets the stored value to the newly specified value.
   //-----------------------------------------------------------------
   public void setValue (int update)
   {
      value = update;
   }

   //-----------------------------------------------------------------
   //  Returns the stored integer value as a string.
   //-----------------------------------------------------------------
   public String toString ()
   {
      return value + "";
   }
}

output1

output2

in main

in memory

passing parameters

Classwork:
1. Why does a3 end up with 333 and not 777?
2. Why a1 isn’t 999 after execution of ParameterTester?
3. Does a1 ever change to 999?
4. Why does a2 change to 888?
5. When does a2 change to 888?
6. Why can a2 change to a different value while a1 and a3 can’t?