Page 341 - Introduction to Programming with Java: A Problem Solving Approach
P. 341
Note that in Figure 8.4a, we call the selectColor method without a reference variable prefix:
this.primary = selectColor("primary");
Why is there no reference variable dot prefix? If you’re in a constructor (or an instance method, for that matter), and you want the current object to call another method that’s in the same class, the reference vari- able dot prefix is unnecessary. Since the constructor and the selectColor method are in the same class, no reference variable dot prefix is necessary.
8.3 HelperMethods 307
/********************************************************
*
Shirt.java
Dean & Dean
This class stores and displays color choices for
a sports-uniform shirt.
********************************************************/
*
*
*
*
import java.util.Scanner;
public class Shirt
{
private String name;
Apago PDF Enhancer
private String primary; // shirt's primary color
private String trim;
// person's name
// shirt's trim color
//*****************************************************
public Shirt()
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter person's name: ");
this.name = stdIn.nextLine();
this.primary = selectColor("primary");
this.trim = selectColor("trim");
// end constructor
//*****************************************************
}
public void display()
{
}
System.out.println(this.name + "'s shirt:\n" +
this.primary + " with " + this.trim + " trim");
// end display
//*****************************************************
No need for a reference variable dot prefix here.
Figure 8.4a
Shirt class—part A