Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

which is why they are often referred to as “variables,” and can change during the operation of your Java game
or IoT application. Alternatively, they can be static (fixed), which makes that data permanent, in which case
it would be called a constant. A constant is a special type of variable, which we will cover in the next section,
because declaring a constant correctly in the Java programming language is a bit more involved (advanced)
than declaring a Java variable.
As far as Java lingo (convention) goes, variables declared at the top of a class are called member
variables, fields, or data fields, although all variables and constants can be considered to be data fields at a
fundamental level.
A variable declared inside of a method, or other lower-level Java programming structure (nested inside
of a class or a method), is called a local variable because it can only be “seen” or used locally, inside of that
programming construct that has been delimited by using curly {...} braces. Finally, variables passed inside
of a parameter list area of a method declaration, constructor method definition, or a method call are, not
surprisingly, called parameters.
A variable is a data field that holds an attribute of your Java object or software, which can (and will)
change during the course of the execution of your software. As you might imagine, this can be especially
important for game programming. The simplest form of variable declaration can be achieved by using one
of the Java data type keywords along with the name that you want to use for that particular variable within
the Java program logic. In the constructor method in the previous section, we declared an integer variable
named scoreIndex to hold the score that your object will accumulate during gameplay. We defined the
variable data type and named it using the following Java variable declaration programming statement:


int scoreIndex; // This could be coded as: int scoreIndex = 0; (default integer value is zero)


As you also saw in the previous section on constructor methods, you can initialize your variable to
a starting value, using the equals operator, along with a data value that matches up with the data type
declared. Here’s an example:


boolean turnActive = false; // Could be: boolean turnActive; (default boolean value is false)


This Java statement declares a boolean data type variable and names it turnActive, on the left side of
your equals operator, and then sets a declared variable to a value of false, which will signify that player’s turn
is not active. This is similar to how an object is declared and instantiated, except the Java new keyword and
constructor method are replaced by the data value itself since now a variable (data field) is being declared
instead of an object being created. We will be covering the different data types (we’ve already covered
integers, Boolean, and Object) in a future section of this chapter.
You can also use Java modifier keywords with variable declarations, which I will do in the next section
of the chapter when I show you how to declare an immutable variable, also known as a constant, which is
fixed or locked into place in memory and which cannot be changed or altered in any way so that it remains,
you guessed it, constant.
We will be covering the Java access modifier keywords, as they pertain to all Java constructs, in the
sections that follow the next section on constants. So, now that I’m almost finished going from the largest
Java constructs, or packages, to the smallest, or data fields, we will start to cover those topics that apply to all
levels (classes, methods, data fields) of Java. These Java concepts will increase in complexity as we progress
through the end of this Java primer chapter, as I wanted to start with easier high-level concepts and drill
down to more complex lower-level ones. At the end of the chapter, we will also cover packaging your Java
project for distribution using the new Java 9 modules feature, which will allow you to optimize the data
footprint for your Pro Java 9 game and make it more secure as well. Java 9 should be released at around the
same time that this book is released to the public, so I am making this book a Java 9 book. Everything in the
Beginning Java 8 Games Development book would still apply to Java 9 development.

Free download pdf