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

■ ■Note If any of the data fields or methods in the superclass that you are extending (or, if you prefer, subclassing) have<br />

been declared using the private access control keyword, those variables (or constants) and methods are reserved for use<br />

only by (or within) that superclass, and thus will not be accessible to your subclass. The same rules apply to nested and<br />

inner classes; these class structures cannot use any code declared as private in the <strong>Java</strong> constructs that contain them<br />

(or that are above them, if you will).<br />

The body of your class is coded inside the curly braces (see Figure 3-2, outermost red box), which follow your<br />

class (and javafx.application.Application superclass, in this case) declaration. This is why you learned about <strong>Java</strong><br />

syntax first, and you are building on that with the class declaration and the <strong>Java</strong> syntax that holds the class definition<br />

(variables, constants, methods, constructor, nested classes) constructs.<br />

As you can see in the figure, the InvinciBagel class extends an Application superclass from the <strong>Java</strong>FX package.<br />

The inheritance diagram (a tool I will be using throughout the book to show you where things come from in the<br />

overall <strong>Java</strong> and <strong>Java</strong>FX API schemas) for your current superclass-to-subclass hierarchy will therefore look like this:<br />

> java.lang.Object<br />

> javafx.application.Application<br />

> invincibagel.InvinciBagel<br />

By extending the javafx.application package and its Application class, you will give the InvinciBagel class<br />

everything it needs to host (or run) the <strong>Java</strong>FX application. The <strong>Java</strong>FX Application class “constructs” an Application<br />

object so that it can use system memory; call an .init() method, to initialize anything that may require initializing; and<br />

call a .start() method (see Figure 3-2, second-outermost red box), which puts things into place that will ultimately be<br />

needed to fire up (start) an InvinciBagel <strong>Java</strong> 8 game application.<br />

When the end user finishes using the InvinciBagel game application, the Application object, created by<br />

the Application class, using the Application() constructor method, will call its .stop() method and remove your<br />

application from system memory, thus freeing up that memory space for other uses by the your end-users. You will<br />

be learning about <strong>Java</strong> 8 methods, constructors, and objects soon, as you are progressing from the high-level package<br />

and class constructs, to lower-level method and object constructs, and so you are moving from a high-level overview<br />

to lower levels. You may be wondering if <strong>Java</strong> classes can be nested inside each other, that is, if <strong>Java</strong> classes contain<br />

other <strong>Java</strong> classes. The answer is yes, they certainly can (and do)! Let’s take a look at the concept of <strong>Java</strong> nested<br />

classes next.<br />

Nested Classes: <strong>Java</strong> Classes Living Inside Other Classes<br />

A nested class in <strong>Java</strong> is a class that is defined inside of another <strong>Java</strong> class. A nested class is part of the class in which<br />

it is nested, and this nesting signifies that the two classes are intended to be used together in some fashion. There<br />

are two types of nested classes: static nested classes, which are commonly referred to simply as nested classes, and<br />

nonstatic nested classes, which are commonly referred to as inner classes.<br />

Static nested classes, which I will refer to as nested classes, are used to create utilities for use with the class that<br />

contains them, and are sometimes used only to hold constants for use with that class. Those of you who develop<br />

Android applications are very familiar with nested classes, as they are quite commonly employed in the Android API,<br />

to hold either utility methods or Android constants, which are used to define things such as screen density settings,<br />

animation motion interpolation curve types, alignment constants, and user interface element scaling settings. If you<br />

are looking for an understanding regarding the concept of static, it can be thought of as fixed, or not capable of being<br />

changed. A photograph is a static image, whereas video is not static. We’ll look at this concept often during this book.<br />

www.it-ebooks.info<br />

49

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

Saved successfully!

Ooh no, something went wrong!