28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

In this version the lifeIndex and hitsIndex variables at the top are initialized to 0, the default value for an<br />

integer, so you do not have to use lifeIndex = 0 or hitsIndex = 0 in the code. The <strong>Java</strong> programming language<br />

accommodates method overloading, so if you use an InvinciBagel bert = new InvinciBagel(900, "W"); method<br />

call to instantiate the InvinciBagel object, the correct constructor method will be used to create the object. The<br />

InvinciBagel object named bert would have a lifeIndex of 900 units of life and no hits on its life, would be facing<br />

West, and would not be currently moving.<br />

You can have as many (overloaded) constructor methods as you like, so long as they are each 100 percent unique.<br />

This means that overloaded constructors must have different parameter list configurations, including parameter list<br />

length (the number of parameters) and parameter list types (order of data types). As you can see, it is the parameter<br />

list (length, data types, order) that allows a <strong>Java</strong> compiler to differentiate overloaded methods from one another.<br />

<strong>Java</strong> Variables and Constants: Values in Data Fields<br />

The next level down (progressing from API, to package, to class, to method, to the actual data values that are being<br />

operated on in <strong>Java</strong> classes and methods) is the data field. Data values are held inside something called a variable; if<br />

you fix, or make permanent, the data, it is called a constant. A constant is a special type of variable (which I will cover<br />

in the next section), because declaring a constant correctly is a bit more involved (advanced) than declaring a <strong>Java</strong><br />

variable.<br />

In the <strong>Java</strong> lingo, variables declared at the top of a class are called member variables, fields, or data fields,<br />

although all variables and constants can be considered data fields, at a fundamental level. A variable declared inside a<br />

method or other lower-level <strong>Java</strong> programming structure declared inside a class or method, is called a local variable,<br />

because it can only be seen locally, inside the programming constructs delimited by curly braces. Finally, variables<br />

declared inside a parameter list area of a method declaration or method call are, not surprisingly, called parameters.<br />

A variable is a data field that holds an attribute of your <strong>Java</strong> object or software that can (and will) change over<br />

the course of time. As you might imagine, this is especially important for game programming. The simplest form of<br />

variable declaration can be achieved by using a <strong>Java</strong> data type keyword, along with the name that you want to use for<br />

the variable in your <strong>Java</strong> program logic. In the previous section, using the constructor method, you declared an integer<br />

variable named hitsIndex to hold the damage, or hits, that your InvinciBagel object will sustain during game play. You<br />

defined the variable data type, and named it, using the following <strong>Java</strong> variable declaration programming statement:<br />

int hitsIndex; // This could also be coded as: int hitsIndex = 0; (the default Integer is Zero)<br />

As you also saw in that section, you can initialize your variable to a starting value, using an equals operator, along<br />

with a data value that matches up with the data type declared: for example:<br />

String facingDirection = "E";<br />

This <strong>Java</strong> statement declares a String data type variable and names it facingDirection, on the left side of the<br />

equals operator, and then sets the declared variable to a value of “E,” which signifies the direction East, or right. This<br />

is similar to how an object is declared and instantiated, except that the <strong>Java</strong> new keyword and constructor method<br />

are replaced by the data value itself, because now a variable (data field) is being declared instead of an object being<br />

created. You will learn about the different data types (I have already covered Integer, String, and Object) later in<br />

chapter (see the section “<strong>Java</strong> Data Types: Defining Data in Applications”).<br />

You can also use <strong>Java</strong> modifier keywords with variable declarations, which I will do in the next section, when I<br />

show you how to declare an immutable variable, also known as a constant, which is fixed, or locked, in memory and<br />

which cannot be altered.<br />

Now that I am almost finished going from the largest <strong>Java</strong> constructs to the smallest (data fields), I will start to<br />

cover topics that apply to all levels (classes, methods, variables) of <strong>Java</strong>. These concepts will generally increase in<br />

complexity as you progress to the end of this <strong>Java</strong> 8 primer chapter.<br />

www.it-ebooks.info<br />

55

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

Saved successfully!

Ooh no, something went wrong!