Data Structures and Algorithms
with Object-Oriented Design Patterns in Java
A variable is a programming language abstraction
that represents a storage location.
A Java variable has the following attributes:
- name
-
The name of a variable is the label
used to identify a variable in the text of a program.
- type
-
The type of a variable determines the set of values
that the variable can have and
the set of operations that can be performed on that variable.
- value
-
The value of a variable is the content
of the memory location(s) occupied by that variable.
How the contents of the memory locations are interpreted is
determined by the type of the variable.
- lifetime
-
The lifetime of a variable is the
interval of time in the execution of a Java program
during which a variable is said to exist.
Local variables exist as long as the method in which they are
declared is active.
Non-static fields of a class exist as long as the object of which
they are members exist.
Static fields of a class exist as long as the class in which
they are defined remains loaded in the Java virtual machine .
- scope
-
The scope of a variable is the range
of statements in the text of a program in which that variable
can be referenced.
Consider the Java variable declaration statement:
int i = 57;
This statement defines a variable
and binds various attributes with that variable.
The name of the variable is i,
the type of the variable is int,
and its initial value is 57.
Some attributes of a variable, such its name and type,
are bound at compile time.
This is called static binding.
Other attributes of a variable, such as its value,
may be bound at run time.
This is called dynamic binding.
There are two kinds of Java variables--local variables and fields.
A local variable
is a variable declared inside a method.
A field
is a variable declared in some class.
(Classes are discussed in Section ).
The type of a Java variable
is either one of the primitive types
or it is a reference type.
Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.