Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

method performs a task but does not return any resulting data to the calling entity. In this case, the calling
entity is the JavaFX Application class since the .start() method is one of the key methods (the others being
the .stop() and .init() methods) provided by the Application superclass that we extended, which controls the
life-cycle stages for your i3D BoardGame JavaFX application.
After the return type, you will supply your method name, which, by convention (programming rules),
should start with a lowercase letter (or word, preferably a verb), with any subsequent (internal) words
(nouns or adjectives) starting with a capital letter. For instance, a method to display the splash screen would
be named .showSplashScreen() or .displaySplashScreen() and, since it does something but does not return a
value, would be declared using this code:


public void displaySplashScreen() { method Java code to display splashscreen goes in here }


If you need to pass parameters, which are named data values that need to be operated on within
the body of your method (the part inside of the curly braces), these go inside of the parentheses that are
attached to the method name. In Figure 5-2, the .start() method for your bootstrap HelloWorld JavaFX
application receives a Stage object, named primaryStage, using the following Java method declaration
syntax:


public void start(Stage primaryStage) { bootstrap Java code to start Application goes in
here }


You can provide as many parameters using the data type and parameter name pairs as you like,
with each pair separated by a comma character. Methods can also have no parameters in which case the
parameter parentheses are empty and the opening and closing parentheses are right next to each other; this
is how I am writing method names in this book so that you know that they are methods. I am using the dot
(notation) before, and the parentheses characters after, the method name, like .start() or .stop() and so forth,
so that you know I am referencing a Java method.
The programming logic that defines your method will be contained inside the “body” of the method,
which as you have already learned is inside the curly braces that define the beginning and the end of the
method. The Java programming logic inside of methods can include variable declarations, program logic
statements, interative control structures, and iterative loops, among other things, all of which we will be
leveraging to create our Java game during this book.


Overloading Your Methods: Providing Unique Parameter Lists


There is another concept in Java that applies to methods that I will cover in this section before I move on,
called overloading Java methods. Overloading the Java method specifically refers to using the same method
name but using different parameter list configurations. What overloading signifies is the Java compiler will
be able to figure out which of your overloaded methods to use, if you have defined more than one method
with the same name.
The Java compiler differentiates overloaded methods by looking at your parameter data types as well
as the order in which they are being passed into the method being called. The Java compiler then uses the
uniqueness of the parameter list as a fingerprint of sorts to discern which of the identically named methods
(that have the same names) to utilize. Therefore, your parameter list configurations must all be completely
unique from each other in order for a Java method overloading feature to be able to work correctly.
We will be learning how to use and how to code Java methods during the course of this book, from
Chapter 6 introducing NetBeans 9 and onward until the end of the book, 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 they are declared
and utilized inside Java classes.

Free download pdf