Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


process from a high-level overview to lower levels as we go along. You may be wondering if Java classes can
be nested inside of each other. That is, can Java classes contain other Java classes? The answer is yes, they
can. Let’s take a closer look at this concept of Java nested classes next.


Nested Classes: Java Classes Living Inside of Other Classes


A nested class in Java is a class that is defined inside of another Java class. A nested class is part of the class
that it is nested inside of, and this nesting signifies that the two classes are intended to be utilized together
in some way. There are two types of nested classes: static nested classes, which are commonly referred to
simply as nested classes, and nonstatic nested classes, which are commonly referred to as inner classes.
Static nested classes, which I will refer to as nested classes, are used to create utilities for use with the
class that contains them and are sometimes used simply to contain constants for use with the class that
contains them. Those of you who develop Android applications are familiar with nested classes, as they’re
quite commonly used in the Android API, either to hold utility methods or to contain Android constants,
which are used to define things like screen density settings, animation motion interpolation curve types,
alignment constants, and user interface element scaling settings, among other things. In Chapter 4 we
covered the concept of static as it relates to games, and for code this has the same meaning or implication.
Java constants can be thought of as fixed, or not capable of being changed.
A nested class uses what is commonly referred to in Java as dot notation in order to reference the
nested class “off of ” its master (or parent) containing class. For instance, MasterClass.NestedClass would
be the referencing format that would be used to reference a nested class using, or via, its master class
(containing class) name, using generic class type names here. If you created your SplashScreen nested class
to draw the splash screen for your Java board game, it would be referenced in your Java code as BoardGame.
SplashScreen by using Java dot notation syntax.
As an example of this, let’s take a look at the JavaFX Application class, which contains the Parameters
nested class. This nested class encapsulates, or contains, the parameters that you can set for your JavaFX
Application. Thus, this Application.Parameters nested class would be part of the same javafx.application
package as your Application class and would be referenced as javafx.application.Application.Parameters
if you were using an import statement.
Similarly, the constructor method (we will be learning about constructor methods soon) would be
written as Application.Parameters() since constructor methods must have the same name as the classes
that they are contained in. Unless you are writing code for other developers to use, which is where nested
classes are most often utilized, like with the JavaFX Application class or the many nested (utility or constant
provider) classes in the Android 8 OS, you are far more likely to utilize nonstatic nested classes. These
nonstatic nested classes are commonly referred to as inner classes for Java games.
A nested class, technically termed a static nested class, is declared using the static keyword (modifier),
which you will be learning about a bit later on during this chapter. So, if you were to create the BoardGame.
SplashScreen nested class, the BoardGame class and the SplashScreen nested class declaration would look
something like the following code:


public class BoardGame extends Application {
static class SplashScreen {
// The Java code that creates and displays your splashscreen is in here
}
}


It is important to note that if you use import javafx.application.Application.Parameters (as an
example) to import the nested class, you can reference the nested class within your class at that point, using
just the Parameters class name, rather than having to use the full class name “path” that shows your class’s
code how to travel through its parent class to the nested class using the Application.Parameter (ClassName.
NestedClassName) reference.

Free download pdf