go to previous page   go to home page   go to next page

Answer:

The same names as can be used for variables (and methods and classes.)


this

Parameter names follow the same rules as the other names you might pick for a program. Names of this type are called identifiers. The rules for identifiers are given in chapter 9.

Here is an altered definition of our HelloObject class:

class HelloObject                                  
{
  private String greeting;

  public HelloObject( String greeting )
  {
    this.greeting = greeting;
  }

  public void speak()                                     
  { 
    System.out.println( greeting );
  }
}

The parameter is named greeting, a legal and sensible name. But the instance variable is also named greeting. This is OK, but can lead to some confusion.

To avoid confusion, use the reserved word this to show when an identifier refers to an object's instance variable.

Instance variables are a permanent part of an object. They are part of its state and exist as long as the object exists. Parameters are used to pass information into a method. They exist while the method is running, but vanish once the method has finished and returned to its caller. (In the corny analogy to the pizza shop, parameters are like order slips passed passed between employees.)


QUESTION 24:

How many constructors does the HelloObject class on this page have?


go to previous page   go to home page   go to next page