Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


We will create a couple of sample constructor methods in this section to show you the basics regarding
how to create a constructor method and what it usually contains. Let’s say you were creating an object for
the game. You could declare a public BoardGame() constructor method by using this following Java code
structure, for instance:


public BoardGame() {
int healthIndex = 1000; // Defines units of Health
int scoreIndex = 0; // Defines units of Scoring
int boardIndex = 0; // Current Game Board Location
boolean turnActive = false; // Flag showing if current turn
}


A constructor method, called using the BoardGame playerName = new BoardGame(); constructor
method call, creates a BoardGame game player object named playerName. The object has 1,000 units of
Health, has no current score because the object is on the first square of the game board, and is not currently
moving because it is not currently their turn.
Next, let’s explore the concept of overloading this constructor method, which we learned about earlier,
and create another constructor method that has parameters that will allow us to define your healthIndex
and turnActive variables of the BoardGame object at the same time that you’re creating it. A constructor
method would look like this:


public BoardGame(int startingHealthIndex, boolean isTurnActive) {
int healthIndex = startingHealthIndex;
int scoreIndex;
int boardIndex;
boolean turnActive = isTurnActive;
}


In this version, I still initialize the scoreIndex and boardIndex variables to zero, which is the default
value for an Integer value, so I do not have to use lifeIndex = 0 or hitsIndex = 0 in this code, just to show
you an optional way to code these two statements. Since the Java programming language accommodates
method overloading, if you use the BoardGame playerOne = new BoardGame(1250, true); method call to
instantiate a BoardGame object, the correct constructor method will be utilized to create the object. This
BoardGame object named playerOne will have a healthIndex of 1250 units of Health, would have a zero
score, would be on the first game board location, and would currently be at their turn.
The Java keyword this can be used to access data fields created using a constructor method. For
instance, within an object’s code, this.startingHealthIndex = value; sets that object’s own internal data field
to the value that you specify. You can also use this() to invoke another constructor method within the same
class construct.
You can have as many (overloaded) constructor methods as you like, as long as they are each 100
percent unique. This means that overloaded constructors must have different parameter list configurations,
including parameter list length (the number of parameters), order, and/or different parameter list types
(different data types). As you can see, it is your parameter list (number of parameters, parameter data types,
and parameter order) that allows a Java compiler to differentiate your overloaded methods from one another.


Java Variables and Constants: Values in Data Fields


The next level down, from API to package to class to method, are the actual data values that are being
operated upon in these Java classes and methods. In Java this is called the data field. Data is held inside of
something that is called a field, just like in database design. Java data fields can be dynamic, or variable,

Free download pdf