The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Creating Classes

Now that we've covered how to create and use objects, and how objects are cleaned up, it's time to show you how to write the classes from which objects are created. This section shows you the main components of a class through a small example that implements a last-in-first-out (LIFO) stack. The following diagram lists the class and identifies the structure of the code.

This implementation of a stack uses another object, a Vector, to store its elements. Vector is a growable array of objects and does a nice job of allocating space for new objects as space is required. The Stack class makes use of this code by using a Vector to store its elements. However, it imposes LIFO restrictions on the Vector-- that is, you can only add elements to and remove elements from the top of the stack.

The Class Declaration

The previous diagram shows that two primary components make up the implementation of a class: the class declaration and the class body. The class declaration declares the name of the class along with other attributes. The class declaration for the Stack class is fairly simple and indicates that the class is public and that its name is Stack. Often, a minimal class declaration such as this one is all you'll need.

However, the class declaration can say more about the class, such as the name of the superclass and if it can be subclassed.

The Class Body

The class body follows the class declaration and is embedded within curly braces { and }. The class body contains declarations for all instance variables and class variables (known collectively as member variables) for the class. In addition, the class body contains declarations and implementations for all instance methods and class methods (known collectively as methods) for the class.

Providing Constructors for Your Classes

A class may contain one or more constructors that provide for the initialization of an object created from the class. This section shows you how to write a constructor.

Declaring Member Variables

A class's state is represented by its member variables. You declare a class's member variables in the body of the class. Typically, you declare a class's variables before you declare its methods, although this is not required.
classDeclaration {
    member variable declarations
    method declarations
}
Note: To declare variables that are members of a class, the declarations must be within the class body, but not within the body of a method. Variables declared within the body of a method are local to that method.

Implementing Methods

As you know, objects have behavior that 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 about writing methods for your Java classes. For more information about how to call methods see Using Objects(in the Learning the Java Language trail).

In Java, you define a class's methods in the body of the class for which the method implements some behavior. Typically, you declare a class's methods after its variables in the class body although this is not required.

Controlling Access to Members of a Class

Member variables and methods are known collectively as members. When you declare a member of a Java class, you can allow or disallow other objects of other types access to that member through the use of access specifiers.

Understanding Instance and Class Members

A Java class can contain two different types of members: instance members and class members. This page shows you how to declare both types of members and how to use them.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.