|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
In the code sample that follows, the method bodies for theisEmptyandpopmethods are shown in bold:Besides regular Java language elements, you can useclass Stack { static final int STACK_EMPTY = -1; Object[] stackelements; int topelement = STACK_EMPTY; . . . boolean isEmpty() { if (topelement == STACK_EMPTY) return true; else return false; } Object pop() { if (topelement == STACK_EMPTY) return null; else { return stackelements[topelement--]; } } }thisin the method body to refer to members in the current object. The current object is the object whose method is being called. You can also usesuperto refer to members in the superclass that the current object has hidden or overriden. Also, a method body may contain declarations for variables that are local to that method.
Typically, within an object's method body you can just refer directly to the object's member variables. However, sometimes you need to disambiguate the member variable name if one of the arguments to the method has the same name.For example, the following constructor for the
HSBColorclass initializes some of an object's member variables according to the arguments passed into the constructor. Each argument to the constructor has the same name as the object's member variable whose initial value the argument contains.You must useclass HSBColor { int hue, saturation, brightness; HSBColor (int hue, int saturation, int brightness) { this.hue = hue; this.saturation = saturation; this.brightness = brightness; }thisin this constructor because you have to disambiguate the argumenthuefrom the member variablehue(and so on with the other arguments). Writinghue = hue;makes no sense. Argument names take precedence and hide member variables with the same name. So to refer to the member variable you must do so through the current object--this--explicitly.Some programmers prefer to always use
thiswhen referring to a member variable of the object whose method the reference appears. Doing so makes the intent of the code explicit and reduces errors based on name sharing.You can also use
thisto call one of the current object's methods. Again this is only necessary if there is some ambiguity in the method name and is often used to make the intent of the code clearer.
If your method hides one of its superclass's member variables, your method can refer to the hidden variable through the use ofsuper. Similarly, if your method overrides one of its superclass's methods, your method can invoke the overriden method throught the use ofsuper.Consider this class:
and its subclass which hidesclass ASillyClass { boolean aVariable; void aMethod() { aVariable = true; } }aVariableand overridesaMethod:Firstclass ASillierClass extends ASillyClass { boolean aVariable; void aMethod() { aVariable = false; super.aMethod(); System.out.println(aVariable); System.out.println(super.aVariable); } }aMethodsetsaVariable(the one declared inASillierClassthat hides the one declared inASillyClass) tofalse. NextaMethodinvoked its overriden method with this statement:This sets the hidden version of thesuper.aMethod();aVariable(the one declared inASillyClass) totrue. ThenaMethoddisplays both versions ofaVariablewhich have different values:false true
Within the body of the method you can declare more variables for use within that method. These variables are local variables and live only while control remains within the method. This method declares a local variableithat it uses to iterate over the elements of its array argument.After this method returns,Object findObjectInArray(Object o, Object[] arrayOfObjects) { int i; // local variable for (i = 0; i < arrayOfObjects.length; i++) { if (arrayOfObjects[i] == o) return o; } return null; }ino longer exists.
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |