Game Development Reference
In-Depth Information
To declare Java variables fixed, you must use a Java final modifier keyword. “Fin-
al” means the same thing as when your parents say that something is final: it is fixed in
place, an FOL (fact of life) , and not going to change, ever. Thus, the first step in creat-
ing a constant is to add this final keyword, placing it in front of the data type keyword
in your declaration.
A convention, when declaring a Java constant (and constants in other programming
languages), is to use uppercase characters , with underscored characters between
each word, which signifies a constant in your code.
If you want to create screen width and screen height constants for your game, you
do so like this:
final int SCREEN_HEIGHT_PIXELS = 480;
final int SCREEN_WIDTH_PIXELS = 640;
If you want all the objects created by your class's constructor method to be able to
see and use this constant, you add the Java static modifier keyword, placing it 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 want only your class, and objects created by this class, to be able to see these
constants, you declare the constants by placing 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 want any Java class, even those outside your package (i.e., anyone else's
Java classes), to be able to see these constants, you declare the constants by placing the
Java public modifier keyword in front of the static modifier keyword, using the follow-
ing Java code:
public static final int SCREEN_HEIGHT_PIXELS = 480;
public static final int SCREEN_WIDTH_PIXELS = 640;
As you can see, declaring a constant involves a significantly more detailed Java
statement than declaring a simple variable for your class! Next, you will take a deeper
look at Java modifier keywords, as they allow you to control things such as access to
Search WWH ::




Custom Search