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

Declaring Member Variables

Stack uses the following line of code to define its member variables:
private Object[] items;
private int top;
This code declares member variables and not other types of variable, such as local variables, because the declaration appears within the class body but outside any methods or constructors. The member variables declared are named items and top. Their data types are array of Object and int respectively. Also, the private keyword identifies items and top as private members. This means that only the Stack class has access to them.

The declaration of items and top are simple member variable declarations, but declarations can be more complex. You can specify not only type, name, and access level but also other attributes, including whether the variable is a class variable and whether it's a constant. The following table shows all the possible components of a member variable declaration.

Variable Declaration Elements
Element Function
accessLevel (Optional) Access level for the variable
static (Optional) Declares a class variable
final (Optional) Indicates that the variable is a constant
transient (Optional) Indicates that the variable is transient
volatile (Optional) Indicates that the variable is volatile
type name The type and name of the variable
Each component of a member variable declaration is further defined and discussed in later sections of this chapter, as follows:
Lets you control what other classes have access to a member variable by specifying one of four access levels: public, protected, package, and private. You control access to methods in the same way. Controlling Access to Members of a Class (in the Learning the Java Language trail) covers access levels in detail.
Declares this is a class variable rather than an instance variable. You also use static to declare class methods. Understanding Instance and Class Members (in the Learning the Java Language trail) talks about declaring instance and class variables.
Indicates that the value of this member cannot change. The following variable declaration defines a constant named PI, whose value is whose value is pi, the ratio of the circumference of a circle to its diameter (3.141592653589793) and cannot be changed:
final double PI = 3.141592653589793;
It's a compile-time error if your program ever tries to change a final variable. By convention, the name of constant values are spelled in uppercase letters.
Marks member variables that should not be serialized. This component is used in object serialization, which is covered in Object Serialization (in the Learning the Java Language trail).
Prevents the compiler from performing certain optimizations on a member. This advanced feature, used by few programmers, is outside the scope of this tutorial.
Like other variables, a member variable must have a type. You can use primitive type names such as int, float, or boolean. Or you can use reference types, such as array, object, or interface names.
A member variable's name can be any legal identifier and, by convention, begins with a lowercase letter. A member variable cannot have the same name as any other member variable in the same class.

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.