The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Classes, Interfaces, and Packages

## The Method Body

> In the code sample that follows, the method bodies for the `isEmpty` and `pop` methods are shown in bold:

> >     class 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--];
> >             }
> >         }
> >     }

> Besides regular Java language elements, you can use `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 use `super` 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.

### 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.

> >     class HSBColor {
> >         int hue, saturation, brightness;
> >         HSBColor (int hue, int saturation, int brightness) {
> >             this.hue = hue;
> >             this.saturation = saturation;
> >             this.brightness = brightness;
> >         }

> You must use `this` in this constructor because you have to disambiguate the argument `hue` from the member variable `hue` (and so on with the other arguments). Writing `hue = 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 `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 of `super`. Similarly, if your method overrides one of its superclass's methods, your method can invoke the overridden method through the use of `super`.

> Consider this class:

> >     class ASillyClass {
> >         boolean aVariable;
> >         void aMethod() {
> >             aVariable = true;
> >         }
> >     }

> and its subclass which hides `aVariable` and overrides `aMethod`:

> >     class ASillierClass extends ASillyClass {
> >         boolean aVariable;
> >         void aMethod() {
> >             aVariable = false;
> >             super.aMethod();
> >             System.out.println(aVariable);
> >             System.out.println(super.aVariable);
> >         }
> >     }

> First `aMethod` sets `aVariable` (the one declared in `ASillierClass` that hides the one declared in `ASillyClass`) to `false`. Next `aMethod` invoked its overridden method with this statement:

> >     super.aMethod();

> This sets the hidden version of the `aVariable` (the one declared in `ASillyClass`) to `true`. Then `aMethod` displays both versions of `aVariable` which have different values:

> >     false
> >     true

### Local 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 variable `i` that it uses to iterate over the elements of its array argument.

> >     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;
> >     }

> After this method returns, `i` no longer exists.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form