Pro Java 9 Games Development Leveraging the JavaFX APIs

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

The Java class can be used to organize your Java code at the next logical level of functional organization,
and therefore, your classes will contain Java code constructs that add specific functionality to a game
application. These include methods, variables, constants, nested classes, or inner classes, all of which will be
covered during this chapter.
Java classes can also be used to create Java objects, which we’ll cover after we learn about classes,
nested classes, methods, and data fields. Java objects are constructed using your Java class. They have the
same name as the Java class and as that class’s constructor method, which we will be covering a bit later
during this chapter.
As you can see in Figure 5-2, you declare your class using a Java class keyword along with a name for
your class. You can also preface the declaration with Java modifier keywords, which we will cover later in
this chapter. Java modifier keywords are always placed before (or, in front of ) the Java class keyword, using
the following format:


class

One of the powerful features of Java classes is that they can be used to modularize your Java game code
so your core game application features can be part of a high-level class, which can be subclassed to create
more specialized versions of that class. Once a class has been used to create a subclass, it then becomes the
superclass, to use Java class hierarchy terminology. A class will usually subclass another superclass using a
Java extends keyword.
Using a Java extends keyword tells the compiler that you want the superclass’s capabilities and
functionality added (extended) to your class, which, once it uses this “extends” keyword, becomes a
subclass. A subclass “extends” the core functionality that is provided by the superclass that it is extending. To
extend the class definition to include a superclass, you add to (or extend, no pun intended) your existing Java
class declaration using the following format:


class extends

When you extend a superclass using your class, which becomes the subclass of that superclass, you
can use all of that superclass’s features (nested classes, inner classes, methods, constructors, variables,
and constants) in your subclass. You can do this without having to explicitly rewrite (recode) these Java
constructs in the body of your class, which would be redundant (and disorganized) because your class
extends the superclass, making it part of your class. We’ll be covering nested and inner classes in the next
section of this chapter, in case you’re wondering what they are.
The body of your class is coded inside of the curly braces (the outer red box, in Figure 5-2), which follow
your class and javafx.application.Application superclass (in this particular case) declaration. This is why you
learned about, or reviewed, Java syntax first; you are building upon that with the class declaration and then
the Java syntax that holds the class definition (variables, constants, methods, constructors, nested classes,
inner classes) constructs.
Notice in Figure 5-2 that the InvinciBagel class extends the Application superclass from a JavaFX application
package. Doing this gives the InvinciBagel class everything that it needs to host, or run, the JavaFX 8 application.
What this JavaFX 8 Application class does is to “construct” your Application object so that it can use system
memory, call an .init() method (to initialize anything that might need initializing), and call the .start() method
that you can see in Figure 5-2 (in the second red box). This .start() method is where you put Java code statements
in place that will ultimately be needed to “fire up” (that is, to start or launch) the InvinciBagel i2D arcade game
Java 8 application. This Java 8 game will also run under Java 9 without modification.
When the end user finishes using the i2D InvinciBagel Java Application, the Application object created
by the Application class, using the Application() constructor method, will call its .stop() method and remove
your application from system memory. This will free up memory space for other uses by an end user. We will
be getting into methods, constructors, and objects soon, as we are progressing from the high-level package
and class constructs to lower-level methods and object constructs so that we can approach the learning

Free download pdf