Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Declaring Member Variables

At minimum, a member variable declaration has two components: the data type of the variable and its name.
type variableName;        // minimal member variable declaration
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 named anInteger within the class IntegerClass.
class IntegerClass {
    int anInteger;
    . . .
    // define methods here
    . . .
}
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.

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 Types(in the Writing Java Programs trail)in 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:

class IntegerClass {
    int anInteger;
    int anInteger() {        // a method with the same name as a member variable
        . . .
    }
}
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.

In short, a member variable declaration looks like this:

[accessSpecifier] [static] [final] [transient] [volatile] type variableName
The items between [ and ] are optional. Italic items are to be replaced by keywords or names.

A member variable declaration defines the following aspects of the variable:

Discussions about final, transient, and volatile variables follow.

Declaring Constants

To create a constant member variable in Java use the keyword final 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:
class Avo {
    final double AVOGADRO = 6.023e23;
}
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.
AvogadroTest.java:5: Can't assign a value to a final variable: AVOGADRO

Declaring 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 the transient 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 the transient 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:

class TransientExample {
    transient int hobo;
    . . .
}
This example declares an integer variable named 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's volatile 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;
    . . .
}


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces