Objects, Classes, and Interfaces |
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 overriden. Also, a method body may contain declarations for variables that are local to that method.this
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 useclass HSBColor { int hue, saturation, brightness; HSBColor (int hue, int saturation, int brightness) { this.hue = hue; this.saturation = saturation; this.brightness = brightness; }this
in this constructor because you have to disambiguate the argumenthue
from the member variablehue
(and so on with the other arguments). Writinghue = hue;
would make 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
this
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
this
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.super
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; } }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 in ASillierClass that hides the one declared in ASillyClass) tofalse
. NextaMethod()
invoked its overriden method with this statement:This sets the hidden version of thesuper.aMethod();aVariable
(the one declared in ASillyClass) totrue
. ThenaMethod
displays both versions ofaVariable
which have different values:false trueLocal Variables
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.
Objects, Classes, and Interfaces |