Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Stack
uses the following line of code to define its member variables: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 namedprivate Object[] items; private int top;items
andtop
. Their data types are array ofObject
andint
respectively. Also, theprivate
keyword identifiesitems
andtop
as private members. This means that only theStack
class has access to them.The declaration of
items
andtop
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.Each component of a member variable declaration is further defined and discussed in later sections of this chapter, as follows:
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
- accessLevel
- 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 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 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
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: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.final double PI = 3.141592653589793;transient
- Marks member variables that should not be serialized. This component is used in object serialization, which is covered in Object Serialization.
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 type names such as
int
,float
, orboolean
. Or you can use reference types, such as array, object, or interface names.- name
- 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.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.