Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


Fixing Data Values in Memory: Defining a Data Constant in Java


If you are already familiar with computer programming, you will know that there is often a need to have
data fields that will always contain the same data value and that will not change during the duration of
the application’s run cycle. These are termed constants and are defined, or declared, using a special
combination of Java access modifier keywords that are used to fix things in memory so that they cannot be
changed. There are also Java modifier keywords that will restrict (or unrestrict) object instances, or access to
certain classes inside or outside of a Java class or package. We will be getting into these in detail in the next
section of the chapter, covering Java modifier keywords.
To declare Java variables as “fixed,” you must use Java’s final modifier keyword. Final means the same
thing as when your parents say something is final; it is fixed in place, a fact of life (FOL), and not going to
change, ever. Thus, the first step in creating a constant is to add this final keyword in front of the data type
keyword in your declaration.
A convention when declaring a Java constant (and constants in other programming languages, as well)
is to use uppercase characters with underscore characters between each word, which signifies a constant
in your code.
If we wanted to create screen width and screen height constants for your game, you would do so like
this:


final int SCREEN_HEIGHT_PIXELS = 480;
final int SCREEN_WIDTH_PIXELS = 640;


There is also a “blank” final, which is a nonstatic final variable whose initialization will be deferred to
your constructor method body. It is also important to note that each object gets its own copy of a nonstatic
final variable.
If you wanted all of the objects created by your class’s constructor method to be able to “see,” and use,
this constant, you would have to also add the Java static modifier keyword in front of the final modifier
keyword, like this:


static final int SCREEN_HEIGHT_PIXELS = 480;
static final int SCREEN_WIDTH_PIXELS = 640;


If you wanted only your class and objects created by this class to be able to see these constants, you
would declare the constants using the Java private modifier keyword in front of the static modifier keyword,
using this code:


private static final int SCREEN_HEIGHT_PIXELS = 480;
private static final int SCREEN_WIDTH_PIXELS = 640;


If you wanted any Java class, even those outside of your package (that is, anyone else’s Java classes), to
be able to see these constants, you would declare the constants using the Java public modifier keyword in
front of the static modifier keyword using the following Java code:


public static final int SCREEN_HEIGHT_PIXELS = 480;
public static final int SCREEN_WIDTH_PIXELS = 640;


As you can see, declaring the constant can be a significantly more detailed Java statement construction
than declaring a simple variable for use in your class. Next we should take a deeper look at Java’s access
modifier keywords since they allow you to control things (like access to classes, methods, constants, and
variables, allowing you to lock a Java code structure from being modified) and similar high-level Java code
control concepts that are fairly complicated.

Free download pdf