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 single member variable:
private Vector items;
This declares a member variable and not some other type of variable (like a local variable) because the declaration appears within the class body but outside of any methods or constructors. The member variable declared is named items, and its data type is Vector. Also, the private keyword identifies items as a private member. This means that only code within the Stack class can access it.

This is a relatively simple member variable declaration, 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 or instance variable and whether it's a constant. Each component of a member variable declaration is further defined below:
accessLevel
Lets you control which other classes have access to a member variable by using 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 covers access levels in detail.
static
Declares this is a class variable rather than an instance variable. You also use static to declare class methods. Understanding Instance and Class Members later in this lesson talks about declaring instance and class variables.
final
Indicates that the value of this member cannot change. The following variable declaration defines a constant named AVOGADRO, whose value is Avogadro's number (6.022 * 1023) and cannot be changed:
final double AVOGADRO = 6.022e23;
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.
transient
The transient marker is not fully specified by The Java Language Specification but is used in object serialization which is covered in Object Serialization (in the Learning the Java Language trail) to mark member variables that should not be serialized.
volatile
The volatile keyword is used to prevent the compiler from performing certain optimizations on a member. This is an advanced Java feature, used by only a few Java programmers, and is outside the scope of this tutorial.
type
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.
name
A member variable's name can be any legal Java identifier and, by convention, begins with a lowercase letter. You cannot declare more than one member variable with the same name in the same class, but a subclass can hide a member variable of the same name in its superclass. Additionally, a member variable and a method can have the same name. For example, the following code is legal:
public class Stack {
    private Vector items;
    // a method with same name as a member variable
    public Vector items() { 
         . . .
    }
}

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

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