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 />

of the .start() method, showing how the method holds the programming logic that creates a basic “Hello World!”<br />

application. The programming logic inside the method uses <strong>Java</strong> programming statements to create a Stage object<br />

and a Scene object, place a button on the screen in a StackPane object, and define event-handling logic, such that<br />

when the button is clicked, the bootstrap <strong>Java</strong> code writes the “Hello World!” text to your NetBeans IDE output area.<br />

The method declaration starts with an access modifier keyword, either public, protected, private, or package<br />

private (which is designated by not using any access control modifier at all). As you can see in the figure, the .start()<br />

method has been declared, using the public access control modifier.<br />

After this access control modifier, you will need to declare the method’s return type. This is the type of data that<br />

the method will return after it is called, or invoked. Because the .start() method performs setup operations but does<br />

not return a specific type of value, it uses the void return type, which signifies that the method performs tasks but does<br />

not return any resulting data to the calling entity. In this case, the calling entity is the <strong>Java</strong>FX Application class, as the<br />

.start() method is one of the key methods (the others being the .stop() and .init() methods) provided by that class to<br />

control the life cycle stages of a <strong>Java</strong>FX application.<br />

Next, you will supply the method name, which, by convention (programming rules), should start with a<br />

lowercase letter (or word, preferably a verb), with any subsequent (internal) words (nouns or adjectives) starting<br />

with a capital letter. For instance, a method to display the splash screen would be named .showSplashScreen() or<br />

.displaySplashScreen() and because it does something but does not return a value, would be declared using<br />

this code:<br />

public void displaySplashScreen() { <strong>Java</strong> code to display splashscreen goes in here }<br />

If you need to pass parameters, which are named data values that have to be operated on within the body of the<br />

method (the part inside the curly braces), these go inside the parentheses that are attached to the method name. In<br />

Figure 3-2 the .start() method for your bootstrap “HelloWorld!” <strong>Java</strong>FX application receives a Stage object, named<br />

primaryStage, using the following <strong>Java</strong> method declaration syntax:<br />

public void start(Stage primaryStage) { bootstrap <strong>Java</strong> code to start Application goes in here }<br />

You can provide as many parameters as you like, using the data type and parameter name pairs, with each pair<br />

separated by a comma. Methods can also have no parameters, in which case the parameter parentheses are empty,<br />

with the opening and closing parentheses right next to each other, for example, .start(), and .stop().<br />

The programming logic that defines your method will be contained in the body of the method, which, as<br />

discussed previously, is inside the curly braces that define the beginning and the end of the method. The <strong>Java</strong><br />

programming logic that is inside methods can include variable declarations, program logic statements, and iterative<br />

control structures (loops), all of which you will be leveraging to create your <strong>Java</strong> game.<br />

Before moving on, let’s focus on one other <strong>Java</strong> concept that applies to methods, namely, overloading <strong>Java</strong><br />

methods. Overloading a <strong>Java</strong> method means using the same method name, but different parameter list configurations.<br />

What this means is that, if you have defined more than one method with the same name, <strong>Java</strong> can figure out which of<br />

your (overloaded) methods to use by looking at the parameters that are being passed into the method being called<br />

and then using that parameter list to discern which of the methods (that have the same name) to use by matching the<br />

parameter list data types and names and the order in which they appear. Of course, your parameter list configurations<br />

must all be unique for this <strong>Java</strong> method overloading feature to work correctly.<br />

You will be learning how to use and how to code <strong>Java</strong> methods over the course of this book, beginning in Chapter 4,<br />

so I am not going to spend too much time on them here, other than to define what they are and the basic rules for how<br />

they are declared, and used, inside <strong>Java</strong> classes.<br />

One specialized kind of method that I am going to cover in detail, however, is the constructor method. This is a<br />

type of method that can be used to create objects. <strong>Java</strong> objects are the foundation of OOP, so you will be taking a look<br />

at constructor methods next, as it is important to do so before learning about the <strong>Java</strong> object itself, which you will<br />

study later in the chapter (see the section “<strong>Java</strong> Objects: Virtual Reality, Using <strong>Java</strong> Constructs“).<br />

52<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!