Java Reference
In-Depth Information
Additionally, class variables may exist and be accessible even when no object of
the class exists. The access qualifier, static , is used to indicate variables or meth-
ods associated with the class rather than an instance. In contrast, an instance
variable exists only when an object — that is, an instance of the class — exists
and its value is not shared among objects. Each object has its own set of values
for instance variables. Changing the value of an instance variable in one object
does not necessarily affect the value of the same variable in another object.
Furthermore, instance variables are often defined as private , limiting their
access to within the class.
Both class and instance variables are defined outside of the method defini-
tions in a class and are in scope, which means that they are accessible from any
method within the class. A local variable , however, is a variable defined within
a method or in the parameter list of a method, and is in scope only within that
method. Local variables should be considered as temporary storage because once
the method exits, they no longer are usable. If the method is executed again, new
local variables are created.
Using the Final Qualifier
Variables, by definition, contain values that can change. In some cases, however,
a programmer needs to ensure that the value of a class or instance variable is not
changed, even inadvertently; the variable should contain a constant value. Such a
variable is sometimes called a named constant , a constant variable , or in Java, a
final variable . Using a final variable allows a constant to have a name, which the
Java program can use in multiple places. Giving a constant a name avoids having
duplicates of a literal, such as 25 or 'a', throughout the program and improves read-
ability and maintainability of the code. In Java, a final variable is declared with
the final qualifier and, by convention, is written using all capital letters.
Final instance variables can be left uninitialized when defined. Such a
variable is called a blank final . The compiler will require the constructor(s) to
initialize such a variable, but each constructor need not supply the same value.
Consequently, the same value may not be used for all instances.
Blank Finals
Blank finals allow objects of the same class to have the same
named constant, but with potentially different values. These
values may be obtained from an external file used to configure
the application. Although very flexible, this is potentially
confusing and, as a consequence, blank finals often are not used.
Figure 9-5 displays the code for the initial comments, the import statement,
and the declaration of class and instance variables, including two final variables.
Lines 1 through 7 add the block comment, line 9 imports the java.util.* package,
and line 11 includes the Password class header. Lines 13 through 16 declare four
class variables, including two final variables, MIN_SIZE and MAX_SIZE, which
set the minimum length of a password to 6 characters and the maximum length
of a password to 15 characters. Lines 18 through 23 declare five instance
variables. Table 9-3 lists the class and instance variables of the Password class.
Search WWH ::




Custom Search