28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

}<br />

}<br />

btn.setText("Say 'Hello World'");<br />

// The other programming statements continue underneath here<br />

When the object declaration and instantiation are split up, they can be placed inside (or outside) methods as<br />

needed for visibility. In the preceding code, other methods of your InvinciBagel class could call the .setText() method<br />

call shown without the <strong>Java</strong> compiler’s throwing an error. The way the Button object is declared in Figure 3-2, only the<br />

.start() method can see the object, and so only the .start() method can use this btn.setText() method call.<br />

Creating a Constructor Method: Coding an Object’s Structure<br />

A constructor method is more a method for creating objects in system memory, whereas other methods (or functions,<br />

if using a different programming language) are usually used to perform calculation or processing of one type or<br />

another. The constructor method’s use in creating <strong>Java</strong> objects in memory, rather than performing some other<br />

programming function, is evidenced by the use of the <strong>Java</strong> new keyword, which creates a new object in memory. For<br />

this reason, a constructor method will define the structure of an object as well as allow the calling entity to populate<br />

the object structure with custom data values, using the constructor method’s parameter list.<br />

You will create a couple of sample constructor methods in this section to learn the basics of how this is done as<br />

well as what a constructor method usually contains. Let’s say you are creating an InvinciBagel object for your game,<br />

so you declare a public InvinciBagel() constructor method, using the following <strong>Java</strong> code structure:<br />

public InvinciBagel() {<br />

int lifeIndex = 1000; // Defines units of lifespan<br />

int hitsIndex = 0; // Defines units of damage ("hits" on the object)<br />

String directionFacing = "E"; // Direction that the object is facing<br />

Boolean currentlyMoving = false; // Flag showing if the object is in motion<br />

}<br />

This constructor method, when called using an InvinciBagel mortimer = new InvinciBagel(); <strong>Java</strong> method<br />

call, creates an InvinciBagel object named mortimer, with 1,000 units of life and no hits, that is facing east and that is<br />

not currently moving.<br />

Next, let’s explore the concept of overloading the constructor method, which you learned about earlier<br />

(see the section “<strong>Java</strong> Methods: <strong>Java</strong> Core Function Code Constructs”), and create another constructor method that<br />

has parameters that allow you to define the lifeIndex and directionFacing variables of the InvinciBagel object while<br />

you are creating it. This constructor method looks like this:<br />

public InvinciBagel(int lifespan, String direction) {<br />

int lifeIndex;<br />

int hitsIndex;<br />

String directionFacing = null;<br />

Boolean currentlyMoving = false;<br />

lifeIndex = lifespan;<br />

directionFacing = direction;<br />

}<br />

54<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!