Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


The Method Declaration

A method's declaration provides a lot of information about the method to the compiler, the runtime system and to other classes and objects. Besides the name of the method, the method declaration carries information such as the return type of the method, the number and type of the arguments required by the method, and what other classes and objects can call the method.

While this may sound like writing a novel rather than simply declaring a method for a class, most method attributes can be declared implicitly. The only two required elements of a method declaration are the method name and the data type returned by the method. For example, the following declares a method named isEmpty in the Stack class that returns a boolean value (true or false):

class Stack {
    . . .
    boolean isEmpty() {
        . . .
    }
}

Returning a Value from a Method

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

Methods can return either values of primitive data types or of reference data types. The isEmpty method in the Stack class returns a primitive 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;
    }
}
However, the pop method in the Stack class returns a reference 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--];
        }
    }
}
Methods use the return operator to return a value. Any method that is not declared void must contain a return statement.

The data type of the value returned by the return statement must match the data type that the method claims to return; you can't return an Object from a method declared to return an integer. When returning an object, the returned object's data type must be either a subclass of or the exact class indicated. When returning an interface type, the object returned must implement the specified interface.

A Method's Name

A method name can be any legal Java identifier. There are three 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 (strings, integers, and so on) 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 parenthesis in the method declaration are arguments to the method. Arguments are covered on the next page: Passing Information into a Method.

    The methods are differentiated by the compiler by the number and type of the arguments passed into the method. Thus, draw(String s) and draw(int i) are distinct and unique methods. You cannot declare more than one method with the same signature: draw(String s) and draw(String t) are identical and will result in a compiler error. You should note that overloaded methods must return the same data type; so void draw(String s) and int draw(String t) declared in the same class will produce a compile-time error.

  2. Any method whose name is the same as its class is a constructor and has a special duty to perform. Constructors are used to initialize a new object of the class type. Constructors can only be called with Java's new operator. You learned how to create an object in Creating Objects. To learn how to write a constructor, see Writing a Constructor Method.
  3. A class may override a method in its superclass. The overriding method must have the same name, return type, and parameter list as the method it overrides. Overriding Methods will show you how to override the methods in your class's superclass.

Advanced Method Declaration Features

Besides the two required elements of a method declaration, a method declaration may contain other elements as well. These elements declare the arguments accepted by the method, whether the method is a class method, and so on.

All told, a method declaration looks like this:

[accessSpecifier] [static] [abstract] [final] [native]
	[synchronized] returnType methodName ([paramlist])
	[throws exceptionsList]
Each of these elements of a method declaration is covered somewhere in this tutorial. The first four links in the following list are in-line in this lesson. If you use the next and previous links at the top and bottom of each page in this lesson, you will ultimately see those four sections.

The last three links in the list cover topics that either warranted their own lesson, or were already included in another lesson. Choosing those links will take you to a different part of this tutorial. If you check out some of that information, be sure to come back!


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces