28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

Fixing Data Values in Memory: Defining a Data Constant in <strong>Java</strong><br />

If you are already familiar with computer programming, you know that there is often a need to have data fields that<br />

will always contain the same data value and that will not change during the duration of your application run cycle.<br />

These are called constants, and they are defined, or declared, using special <strong>Java</strong> modifier keywords that are used<br />

to fix things in memory so that they cannot be changed. There are also <strong>Java</strong> modifier keywords that will restrict (or<br />

unrestrict) object instances, or access to certain classes inside or outside a <strong>Java</strong> class or package (which you will be<br />

examining in detail in the next section).<br />

To declare <strong>Java</strong> variables fixed, you must use a <strong>Java</strong> final modifier keyword. “Final” means the same thing as<br />

when your parents say that something is final: it is fixed in place, an FOL (fact of life), and not going to change, ever.<br />

Thus, the first step in creating a constant is to add this final keyword, placing it in front of the data type keyword in<br />

your declaration.<br />

A convention, when declaring a <strong>Java</strong> constant (and constants in other programming languages), is to use<br />

uppercase characters, with underscored characters between each word, which signifies a constant in your code.<br />

If you want to create screen width and screen height constants for your game, you do so like this:<br />

final int SCREEN_HEIGHT_PIXELS = 480;<br />

final int SCREEN_WIDTH_PIXELS = 640;<br />

If you want all the objects created by your class’s constructor method to be able to see and use this constant, you<br />

add the <strong>Java</strong> static modifier keyword, placing it in front of the final modifier keyword, like this:<br />

static final int SCREEN_HEIGHT_PIXELS = 480;<br />

static final int SCREEN_WIDTH_PIXELS = 640;<br />

If you want only your class, and objects created by this class, to be able to see these constants, you declare the<br />

constants by placing the <strong>Java</strong> private modifier keyword in front of the static modifier keyword, using this code:<br />

private static final int SCREEN_HEIGHT_PIXELS = 480;<br />

private static final int SCREEN_WIDTH_PIXELS = 640;<br />

If you want any <strong>Java</strong> class, even those outside your package (i.e., anyone else’s <strong>Java</strong> classes), to be able to see<br />

these constants, you declare the constants by placing the <strong>Java</strong> public modifier keyword in front of the static modifier<br />

keyword, using the following <strong>Java</strong> code:<br />

public static final int SCREEN_HEIGHT_PIXELS = 480;<br />

public static final int SCREEN_WIDTH_PIXELS = 640;<br />

As you can see, declaring a constant involves a significantly more detailed <strong>Java</strong> statement than declaring a simple<br />

variable for your class! Next, you will take a deeper look at <strong>Java</strong> modifier keywords, as they allow you to control things<br />

such as access to your classes, methods, and variables as well as locking them from being modified and similar highlevel<br />

<strong>Java</strong> code control concepts that are fairly complicated.<br />

<strong>Java</strong> Modifier Keywords: Access Control and More<br />

<strong>Java</strong> modifier keywords are reserved <strong>Java</strong> keywords that modify the access, visibility, or permanence (how long<br />

something exists in memory during the execution of an application) for code inside the primary types of <strong>Java</strong><br />

programming structures. The modifier keywords are the first ones declared outside the <strong>Java</strong> code structure, because<br />

the <strong>Java</strong> logic for the structure, at least for classes and methods, is contained within the curly braces delimiter, which<br />

comes after the class keyword and class name or after the method name and parameter list. Modifier keywords can be<br />

used with <strong>Java</strong> classes, methods, data fields (variables and constants), and interfaces.<br />

56<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!