Objects, Classes, and Interfaces |
At minimum, a member variable declaration has two components: the data type of the variable and its name.A minimal variable declaration is like the declarations that you write for variables used in other areas of your Java programs such as local variables or method parameters. The following code snippet declares an integer member variable namedtype variableName; // minimal member variable declarationanInteger
within the classIntegerClass
.Notice that the member variable declaration appears within the body of the class implementation but not within a method. This positioning within the class body determines that the variable is a member variable.class IntegerClass { int anInteger; . . . // define methods here . . . }Like other variables in Java, member variables must have a type. A variable's type determines the values that can be assigned to the variable and the operations that can be performed it. You should already be familiar with data types in Java through your reading of Variables and Data Typesin the previous lesson.
A member variable's name can be any legal Java identifier and by convention begins with a lower case letter (class names typically begin with upper case letters). You cannot declare more than one member variable with the same name in the same class. However, a member variable and a method can have the same name. For example, the following code is legal:
Besides type and name, you can specify several other attributes for the member variable when you declare it: including whether other objects can access the variable, whether the variable is a class or instance variable, and whether the variable is a constant.class IntegerClass { int anInteger; int anInteger() { // a method with the same name as a member variable . . . } }In short, a member variable declaration looks like this:
The items between [ and ] are optional. Italic items are to be replaced by keywords or names.[accessSpecifier] [static] [final] [transient] [volatile] type variableNameA member variable declaration defines the following aspects of the variable:
Discussions about final, transient, and volatile variables follow.
- accessSpecifier defines which other classes have access to the variable. You control access to methods using the same specifiers, so Controlling Access to Members of a Class covers how you can control access to both member variables and methods.
static
indicates that the variable is a class member variable as opposed to an instance member variable. You also usestatic
to declare class methods. Instance and Class Members talks about declaring instance and class variables and writing instance and class methods.final
indicates that the variable is a constanttransient
variables are not part of the object's persistent statevolatile
means that the variable is modified asynchronouslyDeclaring Constants
To create a constant member variable in Java use the keywordfinal
in your variable declaration. The following variable declaration defines a constant named AVOGADRO whose value is Avogadro's number (6.023 x 10^23) and cannot be changed:By convention, names of constant values are spelled in all capital letters. If your program ever tries to change a constant, the compiler will display an error message similar to the following, and refuse to compile your program.class Avo { final double AVOGADRO = 6.023e23; }AvogadroTest.java:5: Can't assign a value to a final variable: AVOGADRODeclaring Transient Variables
By default member variables are part of the persistent state of the object. Member variables that are part of the persistent state of an object must be saved when the object is archived. You use thetransient
keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object.The JDK 1.0 version of the Java runtime system ignores the
transient
marker. Future releases of the Java system will use thetransient
marker to implement various object archiving functions.Like other variable modifiers in the Java system, you use
transient
in a class or instance variable declaration like this:This example declares an integer variable namedclass TransientExample { transient int hobo; . . . }hobo
that is not part of the persistent state of the TransientExample class.Declaring Volatile Variables
If your class contains a member variable that is modified asynchronously by concurrently running threads, you can use Java'svolatile
keyword to notify the Java runtime system of this.The JDK 1.0 version of the Java runtime system ignores the
volatile
marker. However, future releases of the Java runtime system will use this information to ensure that the volatile variable is loaded from memory before each use, and stored to memory after each use thereby ensuring that the value of the variable is consistent and coherent within each thread.The following variable declaration is an example of how to declare that a variable can be modified asynchronously by concurrent threads:
class VolatileExample { volatile int counter; . . . }
Objects, Classes, and Interfaces |