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 variable:
private List<Object> items;
The member variable declared is named items — its data type is a List of Objects. Also, the private keyword identifies items as a private member. This means that only the Stack class has access to it.

The declaration of items is a 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 variable (static) and whether it is final. 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's value cannot change
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:
accessLevel
Lets you control what other classes have access to a member variable by specifying one of four access levels: public, protected, private, and default (also known as package 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.
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 (in the Learning the Java Language trail) talks about the difference between instance and class variables.
final
Indicates that the value of this member cannot change. A compile-time constant is defined by using static and final together. For example, the following variable declaration defines a constant named PI, whose value is an approximation of π, the ratio of the circumference of a circle to its diameter (3.141592653589793) and and cannot be changed:
static final double PI = 3.141592653589793;
It's a compile-time error if your program ever tries to change a static final variable. By convention, the name of constant values are spelled in uppercase letters.

Note that marking a reference type, such as an array or a list, as final, means that the handle to that object cannot change; however, the elements inside that object can change. With care, you can define a class such that instances of it can not change after being created. Such classes are called immutable and String is a good example of an immutable class. Joshua Bloch describes the pros and cons of immutable classes and advises: "favor immutability" in his book Effective Java (outside of the tutorial).

transient
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).
volatile
Prevents the compiler from performing certain optimizations on a member. This advanced feature, used by few programmers, is outside the scope of this tutorial.
type
Like other variables, a member variable must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as arrays, objects, interfaces, enums.
name
A member variable's name can be any legal identifier and, by convention, non-constant variables begin with a lowercase letter and constants are all uppercase. 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.