Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


as “package private.” If you wanted an inner class to be available outside of the package, you would declare it
to be public, using the following Java code structure:


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


You can also declare an inner class to be protected, which means that it can be accessed by any
subclasses of the parent or containing class. We’ll be getting into Java modifiers after we cover Java methods
and Java variables.
If you declare a class inside of a lower-level Java programming structure that is not a class, such as a
method or an iteration control (commonly called a loop) structure, it would be technically referred to as a
local class. This local class would be visible only inside of that block of code, and as such it does not allow
(or make sense to use) class modifiers such as static, public, protected, or private.
A local class is used like a local variable, except that it is a more complex Java coding construct, rather
than a simple data field value that is used locally. This is not often used in games, as you usually want your
game to be divided “functionally,” into functional classes with methods and variables that clearly are for
a distinct use and reason to keep the complexity of game design and processing clearly defined using the
organization or encapsulation of Java. We will be looking at this throughout the book as we design a different
functional component of the game in each chapter starting in Chapter 6. In this way we are using Java’s
features to their best advantage to create a game design.
Finally, there’s a type of inner class that is called an anonymous class. An anonymous class is a local
class that has not been given any class name. You are likely to encounter anonymous classes far more often
than you are local classes. This is because programmers often do not name their local classes (making them
anonymous classes). The logic local classes contain is only used locally to their declaration, and therefore,
these classes do not really need to have a name, as they are only referenced internally to that block of
Java code.


Java Methods: Core Logic Function Java Constructs


Inside of classes you generally have methods and the data fields (variables or constants) that these methods
utilize. Since we are going from outer structures to inner structures, or top-level structures to lower-level
structures, we will cover methods next. Methods are sometimes called functions in other programming
languages, and you can see an example of the .start() method in Figure 5-2, which shows how the method
holds the programming logic that creates your basic Java game application. The programming logic inside of
the method uses Java programming statements to create a Stage and a Scene, places a button on the screen
in a StackPane, and defines event handling logic so that when the button is clicked, the bootstrap Java code
writes some “Hello World” text to your NetBeans 9 IDE output area.


Declaring Your Method: Modifier, Return Type, and Method Name


The method declaration starts with an access control modifier keyword, either public, protected, private,
or package private (which is designated by not using any access control modifier keyword at all). As you can
see in Figure 5-2, your .start() method has been declared using the public access control modifier. We will be
covering access modifier keywords in greater detail later during this chapter.
After this access control modifier, you will need to declare the method’s return type. This is the type
of data that the method will return, after it is called, or invoked. Since the .start() method performs setup
operations but does not return any specific type of value, it uses a void return type, which signifies that the

Free download pdf