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                                  
{
  String greeting;

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

  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.


QUESTION 23:

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