Previous | Next | Trail Map | Writing Java Programs | Java Objects


Writing a Method

As you know, objects have behaviour and that behaviour is implemented by its methods. Other objects can ask an object to do something by invoking its methods. This section tells you everything you need to know to write methods for your Java classes. For more information about how to call methods see Missing Page.

At minimum a method declaration has a return type indicating the data type of the value returned by the method, a name, and a method body that contains the Java instructions that implement the method:

return_type method_name() {
    . . .
    method body
    . . .
}
For example, the following is a method declaration of a simple method that returns a boolean value (true or false) indicating whether or not something is empty.
boolean isEmpty() {
    . . .
}
Alone this method has no meaning--is what empty? This is because this particular method declaration is not associated with any class. It is the combination of a class and a method that gives meaning to a method. In Java, methods are associated with classes or objects and operate on the class or object with which they are associated. The results you get from the isEmpty() method only have meaning if the method is associated with a stack object, or an array object, or some other type of object that can contain objects or data.

In Java every method must be within a class declaration. An earlier section, Declaring a Class, showed you the general outline of a class declaration:

class declaration {
    . . .
    class body
    . . .
}
Within the class you define all the variables and methods supported by that class. This page focuses on how to declare and implement methods for a class. The next page shows you how to declare your class's methods.

You declare all of the methods that are a member of the class within the class body. Typically, you declare a class's methods after you declare its variables, although this is not required.

class declaration {
    . . .
    member variable declarations
    . . .
    method declarations
    . . .
}
Let's put the isEmpty() method in an appropriate class:
class stack {
    . . .
    boolean isEmpty() {
        . . .
    }
}
Now, we can say that the isEmpty() methods returns true when the stack is empty and returns false otherwise.

The method declaration shown above is a very basic method declaration. Methods have many other attributes such as parameters, access control, and so on.

Returning a Value from a Method

Java requires that a method declare the data type of the value that it returns. If a method returns no value, it can be declared to return void.

There are two major categories of data types in the Java language: simple types and complex types. Simple data types are comprised of a single value and include integer numbers, floating point numbers, boolean values, and characters (a single character, that is). Complex types are comprised of multiple or complex values and, sometimes, the operations that can be performed on those values. Complex types include classes, interfaces, and arrays.

Methods can return either values of simple data types or of complex data types. For example, this method returns a simple data type, a boolean value:

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;
    }
}
The return operator returns the value and must be the last statement in the method. Any method that is not declared void must contain a return statement. The return operator places the indicated value on the stack where the calling method can then retrieve it.

The following method returns a complex data type: an object.

class Stack {
    static final int STACK_EMPTY = -1;
    Object stackelements[];
    int topelement = STACK_EMPTY;
    . . .
    Object pop() {
	if (topelement == STACK_EMPTY)
	    return null;
	else {
	    return stackelements[topelement--];
	}
    }
}
The data type of the value returned by the return statement must match the data type that the method claims to return. For example, you can't return an Object from a method declared to return an integer. When returning an object the object must either derive from, or be the class indicated. If your method returns an interface type, the object returned must implement that interface.

A Method's Name

A method name can be any legal Java identifier. There are two special cases to consider in regards to Java method names.

  1. Java supports method name overloading so multiple methods can share the same name. For example, suppose you were writing a class that can render various types of data to its drawing area. You would need to write a method that knew how to render each data type. In other languages, you would have to think of a new name for each method: drawString(), drawInteger(), drawFloat(), and so on. In Java, you can use the same name for all of the drawing methods but pass in a different type of parameter to each method. So, in your data rendering class, you can declare three methods named draw() each of which takes a different type parameter:
    class DataRenderer {
        void draw(String s) {
            . . .
        }
        void draw(int i) {
            . . .
        }
        void draw(float f) {
            . . .
        }
    }
    

    Note: The information within the ( and ) in the method declaration are parameters to the method. Parameters are covered later in this section in Creating an Object. For more information about how to write a constructor, see Writing a Constructor Method.

A Method's Body

The method body is where all of the action of a method takes place. The method body contains all of the legal Java instructions that implement the method.

Other Method Stuff

Summary

[access_specifier] [static] [abstract] [final] [native] [synchronized] return_value methodname ([paramlist]) [throws exceptionsList]


Previous | Next | Trail Map | Writing Java Programs | Table of Contents