28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

(fixed) nested class will only have one version, which does not change. For instance, a private inner class can only be<br />

used by a parent class that contains it. The SplashScreen inner class coded as a private class would look something<br />

like this:<br />

public class InvinciBagel extends Application {<br />

private class SplashScreen {<br />

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

}<br />

}<br />

Because this class is declared as private, it is for your own application usage (the containing class’s usage,<br />

specifically). Thus, this would not be a utility or constant class for use by other classes, applications, or developers.<br />

You can also declare your inner class without using the private access modifier keyword, which would look like the<br />

following <strong>Java</strong> programming construct:<br />

public class InvinciBagel extends Application {<br />

class SplashScreen {<br />

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

}<br />

}<br />

This level of access control is called package or package private and is the default level of access control<br />

applied to any class, interface, method, or data field that is declared without using one of the other <strong>Java</strong> access control<br />

modifier keywords (public, protected, private). This type of inner class can be accessed not only by the top-level,<br />

or containing, class, but also by any other class member of the package that contains that class. This is because the<br />

containing class is declared public, and the inner class is declared package private. If you want an inner class to be<br />

available outside the package, you declare it to be public, using the following <strong>Java</strong> code structure:<br />

public class InvinciBagel extends Application {<br />

public class SplashScreen {<br />

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

}<br />

}<br />

You can also declare an inner class protected, meaning that it can only be accessed by any subclasses of the<br />

parent class. If you declare a class inside a lower-level <strong>Java</strong> programming structure that is not a class, such as a method<br />

or an iteration control structure (commonly called a loop), it would technically be referred to as a local class. A local<br />

class is only visible inside that block of code; thus, it does not allow (or make sense to use) class modifiers, such as<br />

static, public, protected, or private. A local class is used like a local variable, except that it is a complex <strong>Java</strong> coding<br />

construct rather than a simple a data field value that is used locally.<br />

Finally, there is a type of inner class called an anonymous class. An anonymous class is a local class that has not<br />

been given a class name. You are likely to encounter anonymous classes far more often than you are local classes.<br />

This is because programmers often do not name their local classes (making them anonymous classes); the logic local<br />

classes contain is only used locally, to their declaration, and so these classes do not really need to have a name—they<br />

are only referenced internally to that block of <strong>Java</strong> code.<br />

<strong>Java</strong> Methods: Core <strong>Java</strong> Function Code Constructs<br />

Inside classes, you generally have methods and the data fields (variables or constants) that these methods use.<br />

Because we are going from outside to inside, or top-level structures to lower-level structures, I will cover methods<br />

next. Methods are sometimes called functions in other programming languages. Figure 3-2 provides an example<br />

www.it-ebooks.info<br />

51

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

Saved successfully!

Ooh no, something went wrong!