Previous | Next | Trail Map | Writing Java Programs | Java Objects


Declaring a Member Variable

[PENDING: fix intro to this page] In Java, there are two different kinds of variables: variables that are associated with an object or class (called member variables) and those that are not associated with an object or class but are used locally within methods and other code blocks (local variables, parameters, etc.). This page talks about member variables. For more information regarding local and other variables, see missing page.

You define all member variables within the body of the class. The following is a general description of all member variable declarations in Java.

[accessSpecifier] [static] [final] [transient] [volatile] type variablename
There are two different types of member variables: class variables and instance variables. A class variable occurs once per class regardless of the number of instances created of that class. The system allocates memory for class variables the first time it encounters the class. An instance variable occurs once per instance of a class; that is, every time you create a new instance of the class, the system allocates memory for all of the class's instance variables.

An earlier section, Declaring a Class, showed you the general outline of a class declaration:

class declaration {
    . . .
    class body
    . . .
}
Within the class you define all the variables and methods supported by that class. This page focuses on how to declare variables for a class. The next page shows you how to declare and implement your class's methods.

You declare all of the variables that are a member of the class within the class body. Typically, you declare a class's variables before you declare its methods, although this is not required.

class declaration {
    . . .
    member variable declarations
    . . .
    method declarations
    . . .
}
Note: To declare variables that are a member of a class, the declarations must be within the class body, but not within the body of a method. Variables declared within the body of a method are local to that method.

At minimum, a member variable declaration has two components: the type of the variable and its name. For example, the following code snippet declares an integer variable named anInteger for the class IntegerClass.

class IntegerClass {
    int anInteger;
}
When you declare a variable like this, you declare an instance variable. [PENDING: More more more]. For information about declaring class variables, see

A Variable's Type

Every variable in a Java program must have a type. A variable's type determines the values that the variable can have and the operations that can be performed on the variable. There are two major categories of data types in the Java language: simple types and complex types. Simple data types are comprised of a single value and include integer numbers, floating point numbers, boolean values, and characters (a single character, that is). Complex types are comprised of multiple or complex values and, sometimes, the operations that can be performed on those values. Complex types include classes, interfaces, and arrays.

The following table lists all of the simple data types supported by the Java language, the keyword that you use to declare a variable of that type, and the size of the variable.

Keyword		Size	Description
----------------------------
(integer types)
byte		8-bit	Byte-length integer
short		16-bit	Short integer
int		32-bit	Integer
long		64-bit	Long integer

(floating-point types)
float		32-bit	single-precision floating point
double		64-bit	double-precision floating point

(other types)
char		16-bit	a single Unicode character
boolean		N/A	a Boolean

[PENDING: Say something intelligent about complex data types.]

A Variable's Name

[PENDING: More here]

Other Variable Declaration Components

Besides type and name, there are several other variable attributes that you can specify when declaring a variable. These include such attributes as whether other objects can access the variable, whether or not the variable is a class or instance variable and whether or not the variable is a constant.

Follow these links to find more information about

Summary

In summary, 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 variable declaration defines the following aspects of the variable:


Previous | Next | Trail Map | Writing Java Programs | Table of Contents