Game Development Reference
In-Depth Information
int hitsIndex; // This could also be coded as: int
hitsIndex = 0; (the default Integer is Zero)
As you also saw in that section, you can initialize your variable to a starting value,
using an equals operator, along with a data value that matches up with the data type
declared: for example:
String facingDirection = "E";
This Java statement declares a String data type variable and names it facingDirec-
tion , on the left side of the equals operator, and then sets the declared variable to a
value of “E,” which signifies the direction East , or right. This is similar to how an ob-
ject is declared and instantiated, except that the Java new keyword and constructor
method are replaced by the data value itself, because now a variable (data field) is be-
ing declared instead of an object being created. You will learn about the different data
types (I have already covered Integer, String, and Object) later in chapter (see the sec-
tion “Java Data Types: Defining Data in Applications”).
You can also use Java modifier keywords with variable declarations, which I will
do in the next section, when I show you how to declare an immutable variable, also
known as a constant , which is fixed, or locked , in memory and which cannot be
altered.
Now that I am almost finished going from the largest Java constructs to the smallest
(data fields), I will start to cover topics that apply to all levels (classes, methods, vari-
ables) of Java. These concepts will generally increase in complexity as you progress to
the end of this Java 8 primer chapter.
Fixing Data Values in Memory: Defining a Data Con-
stant in Java
If you are already familiar with computer programming, you 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 your application run cycle. These are called constants ,
and they are defined, or declared, using special Java 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 a Java class or package (which you will be examining in detail in the
next section).
Search WWH ::




Custom Search