THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

System.out.println(Body.nextID);


The use of System.out itself illustrates accessing a static field. When a static member of a class is
referenced many times from another class, or when multiple static members of a class are referenced from
another class, you can clarify the code, and perhaps save some typing, by using a static import statement to
name the class of the static member once. This is discussed in detail in Section 2.9 on page 71.


A static member may also be accessed using a reference to an object of that class, such as


System.out.println(mercury.nextID);


You should avoid this form because it gives the false impression that nextID is a member of the object
mercury, not a member of the class Body. It is the type of the reference, not the type of the object it refers
to, that determines the class in which to look for the static variable.


In this book when we use the term field, we usually mean the non-static kind. When the context makes it
ambiguous, we use the term non-static field to be clear.


Exercise 2.3: Add a static field to your Vehicle class to hold the next vehicle identification number, and a
non-static field to the Vehicle class to hold each car's ID number.


2.2.3. final Fields


A final variable is one whose value cannot be changed after it has been initializedany attempt to assign to
such a field will produce a compile-time error. We have seen final fields used to define named constants
because constants don't change value. In general, a final field is used to define an immutable property of a
class or objecta property that doesn't change for the lifetime of the class or object. Fields that are marked
final also have special semantics with regard to concurrent access by multiple threads, this is discussed in
more detail in Chapter 14.


If a final field does not have an initializer it is termed a blank final. You would use a blank final when
simple initialization is not appropriate for a field. Such fields must be initialized once the class has been
initialized (in the case of static final fields) or once an object of the class has been fully constructed (for
non-static final fields). The compiler will ensure that this is done and refuse to compile a class if it
determines that a final field does not get initialized.


A final field of a primitive type, or of String type, that is initialized with a constant expressionthat is, an
expression whose value can be determined at compile timeis known as a constant variable. Constant variables
are special because the compiler treats them as values not fields. If your code refers to a constant variable, the
compiler does not generate bytecode to load the value of the field from the object, it simply inserts the value
directly into the bytecode. This can be a useful optimization, but it means that if the value of your final field
needs to be changed, then every piece of code that refers to that field also has to be recompiled.


Whether a property is immutable is determined by the semantics of the application for which the class was
designed. When you decide whether a field should be final, consider three things:



  • Does the field represent an immutable property of the object?

  • Is the value of the field always known at the time the object is created?

  • Is it always practical and appropriate to set the value of the field when the object is created?

Free download pdf