Pro Java 9 Games Development Leveraging the JavaFX APIs

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

As you’ll see many times throughout this book, methods can also be accessed using dot notation.
So, instead of using ClassName.NestedClassName.MethodName, you could, if you had used the import
statement to import this nested class, simply use NestedClassName.MethodName. This is because the
import statement has already been used to establish the full “reference path” to this nested class, through its
containing class, so you don’t have to.
Next, let’s take a look at nonstatic nested classes, which are more commonly referred to as inner
classes.


Inner Classes: Different Types of Nonstatic Nested Classes


Java inner classes are also nested classes, but they are not declared using the static keyword modifier before
the class keyword and class name, which is why they are called “nonstatic” nested classes. Thus, any class
declaration that is inside another class that doesn’t use the static (keyword) modifier would be termed an
inner class in Java. There are three types of inner classes in Java: the member class, the local class, and
the anonymous class. We’ll cover what the differences are between these inner class types, as well as how
they’re implemented, in detail during this section.
Like nested classes, Member classes are defined within the body of your containing (parent) class. You
can declare a member class anywhere within the body of the containing class. You would want to declare a
member class when you wanted to access data fields (variables or constants) and methods belonging to the
containing class without having to provide a path (via dot notation) to the data field or method (ClassName.
DataField or ClassName.Method). A member class can be thought of as a nested class that does not use the
Java static modifier keyword.
Whereas a nested class is referenced through its containing or “top-level” class, using a dot notation
path to the static nested class, a member class, since it is not static, is “instance-specific,” which means that
objects (instances) created using that class can be different from each other (an object is a unique “instance”
of a class), whereas a static (fixed) nested class will have only one version that doesn’t change. For instance,
a private inner class can only be used by a parent class that contains it. The SplashScreen inner class coded
as a private class would look something like this:


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


Since this is declared as private, it is for our own application usage (the containing class’s usage
specifically). Thus, this would not be a utility or constant class for use by other classes, applications, or
developers. You can also declare your inner class without using the private access modifier keyword, which
would look like the following Java programming construct:


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


This level of access control is called package or package private and is the “default” level of access
control that is applied to any class, interface, method, or data field that is declared without using one of the
other Java access control modifier keywords (public, protected, or private). This type of inner class can be
accessed not only by the top-level or containing class but also by any other class member of the package that
contains that class. This is because the containing class is declared as “public” and the inner class is declared

Free download pdf