Java Reference
In-Depth Information
Constant attribute
The attributes that refer to the component object are initialized inline
and are declared final.
class Container {
final Component component # new Component();
Force resolution
The attribute can be used to navigate to the component. The component
creation is automatic at container creation. The Java language ensures
that the link cannot be modified. The syntax is compact and readable.
Design rationale
The advantages of this approach are discussed in Sidebar 5.1.
A constant value is used in multiple places inside a class and
or across
several classes.
How to ensure a consistent and easily updatable use of the constant.
Forces or tradeoffs
The constant value must be the same in all places.
Changing the value has effects in several places.
A misspelling in the constant value is difficult to identify and can have
dramatic effects.
The meaning of the constant should be clear.
The value of the constant must remain the same (as opposed to a variable
Constant attributes can be defined.
The attribute must be qualified as final static .
The attribute must be initialized inline.
The name of the attribute is all upper case, with words separated by the
underscore character “_”.
final static String READ_COMMAND # "RAM_READ";
Force resolution
Using the same attribute ensures that the same value is used in all the
There is a single point of change that is automatically reflected in all the
places where the constant is used.
It is impossible to misspell the constant. The name of the attribute provides
the meaning of the constant. The attribute value cannot be changed.
Design rationale
The static qualifier ensures that all the references to the attribute share
the same value. The final qualifier ensures that the attribute cannot be
changed at run time.
The value of the constant is defined at a single point; this value is used
through a reference to the final static attribute. It is not possible to
misspell the value because it is written only once, and any misspelling of
the attribute name is found by the compiler.
The naming convention is part of the Java coding convention and helps to
distinguish constants from variables.
Hayes, J. P. (1998) Computer Architecture and Organization , 3rd edn, McGraw-Hill
Education, ISE Editions.
Search WWH ::

Custom Search