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

A nested class uses what is commonly referred to in <strong>Java</strong> as dot notation to reference the nested class “off of”<br />

its master, or parent, containing class. For instance, MasterClass.NestedClass would be the referencing format that<br />

would be used to reference a nested class via its master class (containing class) name, using generic class type names<br />

here. If you created an InvinciBagel SplashScreen nested class to draw the splash screen for your <strong>Java</strong> game, it would<br />

be referenced in your <strong>Java</strong> code as InvinciBagel.SplashScreen, using this <strong>Java</strong> 8 dot notation syntax.<br />

Let’s take a look at, for example, the <strong>Java</strong>FX Application class, which contains a Parameters nested class. This<br />

nested class encapsulates, or contains, the parameters that you can set for your <strong>Java</strong>FX application. Thus, this<br />

Application.Parameters nested class would be a part of the same javafx.application package as your Application<br />

class and would be referenced as javafx.application.Application.Parameters, if you were using an import statement.<br />

Similarly, the constructor method would be written as Application.Parameters(), because the constructor<br />

method must have the exact same naming schema as the class that it is contained in. Unless you are writing code for<br />

other developers, which is when nested classes are most often used (such as the <strong>Java</strong>FX Application class or the many<br />

nested utility or constant provider classes which you will find in the Android OS), you are far more likely to utilize<br />

non-static nested classes (commonly referred to as <strong>Java</strong> inner classes).<br />

A nested class can be declared by using the <strong>Java</strong> static keyword. A <strong>Java</strong> keyword is also sometimes called a <strong>Java</strong><br />

modifier. Therefore, if you were to do an InvinciBagel.SplashScreen nested class, the InvinciBagel class and its<br />

SplashScreen nested class declaration (<strong>Java</strong> 8 programming structure) would look something like this:<br />

public class InvinciBagel extends Application {<br />

static class SplashScreen {<br />

// The <strong>Java</strong> code that creates and displays your splashscreen is in here<br />

}<br />

}<br />

It is important to note if you use, for example, import javafx.application.Application.Parameters to import<br />

a nested class, you can reference that nested class within your class, using just the Parameters class name, rather than<br />

the full class name path that shows your class’s code how to travel through a parent class to its nested class via the<br />

Application.Parameter (ClassName.NestedClassName) dot notation syntax reference.<br />

As you will see many times throughout this book, <strong>Java</strong> methods can also be accessed using the dot notation. So,<br />

instead of using ClassName.NestedClassName.MethodName, you could, if you had used the import statement to<br />

import this nested class, simply use NestedClassName.MethodName. This is because the <strong>Java</strong> import statement has<br />

already been used to establish the full reference path to this nested class, through its containing class, and so you do<br />

not have to provide this full path reference for the compiler to know what code construct you are referring to!<br />

Next, let’s take a look at nonstatic nested classes, which are usually referred to as <strong>Java</strong> inner classes.<br />

Inner Classes: Different Types of Nonstatic Nested Classes<br />

<strong>Java</strong> inner classes are also nested classes, but they are not declared using the static keyword modifier before the class<br />

keyword and class name, which is why they are called nonstatic nested classes. Thus, any class declaration that is<br />

inside another class that does not use the static (keyword) modifier would be termed an inner class in <strong>Java</strong>. There are<br />

three types of inner classes in <strong>Java</strong>: member class, local class, and anonymous class. In this section, you will discover<br />

what the differences are between these inner classes, as well as how they are implemented .<br />

Like nested classes, member classes are defined within the body of the containing (parent) class. You can<br />

declare a member class anywhere within the body of the containing class. You would declare a member class if you<br />

wanted to access data fields (variables or constants) and methods belonging to the containing class without having to<br />

provide a path (via dot notation) to the data field or method (ClassName.DataField or ClassName.Method).<br />

A member class can be thought of as a nested class that does not use the <strong>Java</strong> static modifier keyword.<br />

Whereas a nested class is referenced through its containing, or top-level, class, using a dot notation path to the<br />

static nested class, a member class, because it is not static, is instance specific, meaning that objects (instances)<br />

created via that class can be different from each other (an object is a unique instance of a class), whereas a static<br />

50<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!