Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Stack
uses the following line of code to define its member variable:The member variable declared is namedprivate List<Object> items;items
its data type is aList
ofObject
s. Also, theprivate
keyword identifiesitems
as a private member. This means that only theStack
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.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'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
- 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 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 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
andfinal
together. For example, the following variable declaration defines a constant namedPI
, whose value is an approximation of π, the ratio of the circumference of a circle to its diameter (3.141592653589793) and and cannot be changed: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.static final double PI = 3.141592653589793;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 andString
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.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 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.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.