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

This section shows you the main components of a class by using a small example that implements a stack — a data structure whose items are added and removed in a last-in-first-out (LIFO) fashion. The following figure shows the Stack class and identifies the elements of a class.

The Stack Class

A class definition has two main components: the class declaration and the class body. The class declaration is the first line of code in a class. At a minimum, the class declaration declares the name of the class.

The class body follows the class declaration and appears between braces — { and }. The class body contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the variables that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects. The Stack class defines two member variables within the class body — the items list and top, the array index of the top of the stack. The Stack class also defines one constructor which takes one argument and three methods: push, pop, and isEmpty.


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

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