Game Development Reference
In-Depth Information
ing the Java final keyword. If we were to do this, the basic Java 8 syntax for the new
declaration and instantiation statement for the CURRENT_CAST object will be written
as follows:
private final List<Actor> CURRENT_CAST = new
ArrayList<>();
Figure 10-5 . Mouse-over yellow warning highlight under CURRENT_CAST, and use the Alt-Enter dialog to fix prob-
lem
Often there is a misunderstanding as to the use of this Java modifier keyword final
with regard to objects . It is true that with most Java variables (numeric, Boolean, and
String) when made final, the variable value itself can't be changed. Many assume that a
final modifier when used with a Java object (variable declaration) also makes the ob-
ject itself “final,” and therefore “immutable,” or not changeable in memory.
Generally in Java the final keyword when used with an object variable refers to
memory references , and the immutable keyword applies to those objects themselves,
and means that they can't be changed. Therefore, an object (reference) that is declared
as final can still contain an object that is mutable (can be changed, as we wish to do
here).
In fact, what the final modifier keyword does regarding Java objects in memory,
such as our CURRENT_CAST ArrayList<Actor> object, is to make the reference to
where it's being kept in memory locked, that is, finalized. So what NetBeans is sug-
 
Search WWH ::




Custom Search