28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

Often there is a misunderstanding as to the use of this <strong>Java</strong> modifier keyword final with regard to objects. It is<br />

true that with most <strong>Java</strong> variables (numeric, Boolean, and String) when made final, the variable value itself can’t be<br />

changed. Many assume that a final modifier when used with a <strong>Java</strong> object (variable declaration) also makes the object<br />

itself “final,” and therefore “immutable,” or not changeable in memory.<br />

Generally in <strong>Java</strong> the final keyword when used with an object variable refers to memory references, and the<br />

immutable keyword applies to those objects themselves, and means that they can’t be changed. Therefore, an object<br />

(reference) that is declared as final can still contain an object that is mutable (can be changed, as we wish to do here).<br />

In fact, what the final modifier keyword does regarding <strong>Java</strong> objects in memory, such as our CURRENT_CAST<br />

ArrayList object, is to make the reference to where it’s being kept in memory locked, that is, finalized. So<br />

what NetBeans is suggesting here is an optimization that will allow your CURRENT_CAST data store object to remain<br />

where it is in memory all of the time (after it is created).<br />

This does not mean that your List ArrayList object itself can’t change. Your game’s <strong>Java</strong> code can<br />

expand, contract, and clear (reset) a List ArrayList object that has been declared final at any time, based<br />

upon your game’s invocation of the .addCurrentCast(), .removeCurrentCast(), and .resetCurrentCast() methods.<br />

The optimization theory here would be the more a JVM can “lock down” memory locations “up front” (upon<br />

program launch, when loading into memory), the better it can optimize memory, as well as the CPU cycles that are<br />

needed to access this memory. If you think about it, if the CPU does not have to “look” for an object in memory, then<br />

it will be able to access it faster. A final object can also be used more optimally in a multi-threaded environment.<br />

If, however, you do not want to make your object references final, you can optionally turn this feature off in<br />

NetBeans. This can be done using the Tools ➤ Options menu sequence, seen on the left side of Figure 10-6, in order<br />

to access the Options dialog, which is shown in the right side of Figure 10-6. As you can see along the top of this<br />

Options dialog, NetBeans has organized it’s hundreds of preferences (also known as options) into ten specific areas,<br />

and even has a Search Filter, seen at the top right of the dialog as well, in case you don’t know exactly where to look<br />

for a given option. If any of these sections have too many options to display on the dialog screen, there will be tabs<br />

(the hints tab is shown selected in Figure 10-6), which you can use to navigate to an area you want to visit. We’re<br />

going to the Hints section, and selecting the <strong>Java</strong> Language from the drop-down, and then finally opening up the<br />

Threading section.<br />

Figure 10-6. Setting Editor Hints Preferences using the Tools ➤ Options menu and the Editor ➤ Hints ➤ <strong>Java</strong> ➤ Threading<br />

Now that we have covered the final object variable issue, and shown you both ways of dealing with it, let’s continue on,<br />

and create a second ArrayList object named COLLIDE_CHECKLIST to store complex collision data.<br />

217<br />

www.it-ebooks.info<br />

x

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!