Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
In the code sample that follows, the method bodies for theisEmpty
andpop
methods 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--]; } } }this
in 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 usesuper
to refer to members in the superclass that the current object has hidden or overridden. 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
HSBColor
class 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 use theclass HSBColor { int hue, saturation, brightness; HSBColor (int hue, int saturation, int brightness) { this.hue = hue; this.saturation = saturation; this.brightness = brightness; }this
keyword in this constructor because you have to disambiguate the argumenthue
from 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--using thethis
keyword to refer to the current object--explicitly.Some programmers prefer to always use the
this
keyword when 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 the
this
keyword to 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 of thesuper
keyword. Similarly, if your method overrides one of its superclass's methods, your method can invoke the overridden method through the use of thesuper
keyword.Consider this class:
and its subclass which hidesclass ASillyClass { boolean aVariable; void aMethod() { aVariable = true; } }aVariable
and overridesaMethod
:Firstclass ASillierClass extends ASillyClass { boolean aVariable; void aMethod() { aVariable = false; super.aMethod(); System.out.println(aVariable); System.out.println(super.aVariable); } }aMethod
setsaVariable
(the one declared inASillierClass
that hides the one declared inASillyClass
) tofalse
. NextaMethod
invoked its overridden method with this statement:This sets the hidden version of thesuper.aMethod();aVariable
(the one declared inASillyClass
) totrue
. ThenaMethod
displays both versions ofaVariable
which 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 variablei
that 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; }i
no longer exists.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.