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


The Class Body

An earlier section, Creating Your Own Classes, showed you the general outline of a class implementation:
classDeclaration {
    . . .
    classBody
    . . .
}
The previous page described all of the components of the class declaration. This page describes the general structure and organization of the class body.

The class body component of a class implementation can itself contain two different sections: variable declarations and methods. A class's member variables represent a class's state and its methods implement the class's behaviour. Within the class body you define all the member variables and methods supported by your class.

Typically, you declare a class's member variables first and then you provide the method declarations and implementations, although this is not required.

classDeclaration {
    . . .
    memberVariableDeclarations
    . . .
    methodDeclarations
    . . .
}
For more information about how to declare member variables, see Declaring a Member Variable. And for more information about how to implement methods, see Writing a Method.

In addition to the member variables and methods you explicitly declare within the class body, your class may also inherit some from its superclass. For example, every class in the Java environment is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behaviours that all objects must have such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and so on. Thus, as descendents of this class, all objects in the Java environment inherit this behaviour from the the Object class.

Special Methods within a Class

There are two special methods that you can declare within the class body: constructors and the finalize() method. For more information follow the links provided below:


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