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


Creating Your Own Classes

Now that you know how to create, use, and destroy objects, it's time to learn how to write the classes from which objects can be created.

A class is a template that you can use to create many objects. The implementation of a class is comprised of two components: the class declaration and the class body.

classDeclaration {
    . . .
    classBody
    . . .
}

The Class Declaration

The class declaration component declares the name of the class along with other attributes such as the class's superclass, and whether the class is public, final, or abstract. The class declaration must contain the class keyword and the name of the class that you are defining. Thus the minimum class declaration looks like this:
class NameOfClass {
    . . .
}

The Class Body

The class body follows the class declaration and is embedded within curly braces { and }. The class body component contains declarations for the class's member variables, and declarations and implemenatations for the class's methods. This section includes a discussion on two special methods that you can declare within the class body: Constructors and Writing a finalize() Method.


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