Pro Java 9 Games Development Leveraging the JavaFX APIs

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

constructors in a class to be accessed only inside of that class, and as of Java 9, private interfaces are now
allowed. This private access control keyword allows Java to implement a concept called encapsulation,
where a class (and objects created using that class) can encapsulate itself, hiding its “internals” from the
outside Java universe, so to speak. This encapsulation is further enhanced in Java 9 using modules, which
we’ll be covering toward the end of this chapter. The OOP concept of encapsulation can be used to allow
teams to create (and debug) their own classes and objects. In this way, no one else’s Java code can break
code that exists inside of a class because its methods, variables, constants, interfaces, and constructors are
private. Encapsulation can also be utilized to protect code and resources (assets) from public access.
This access modifier keyword essentially “privatizes” methods or variables in a class so that they can
be used locally only within that class or by objects created by that class’s constructor methods. Unless you
own the class that these private Java constructs are inside of, you cannot access, or utilize, these methods
or data fields. This is the most restrictive level of access control in Java. A variable declared as private can
be accessed outside of the class, if a public method that accesses a private variable from inside of the class,
called a public .get() method call, is declared as public and thus provides a pathway (or doorway) through
that public method to the data in the private variable or constant.


Java Package Private Modifier: Variables, Methods, or Classes in the Package


If no Java access control modifier keyword is declared, then a default access control level, which is also
referred to as the package private access control level, will be applied to that Java construct (class, method,
data field, constructor, or interface). This means that these package private Java constructs are visible, or
available, to any other Java class that is inside of that Java package that contains them. This package private
level of access control is the easiest to apply to your classes, interfaces, methods, constructors, constants,
and variables since it is applied as a default action by simply not explicitly declaring any Java access control
modifier keyword before your Java construct.
You will use this default package private access control level quite a bit for your own pro Java games
and IoT applications programming, as usually you are creating your own application in your own package
for your users to use in a completed, compiled, executable state with Java 9’s new enhanced security Java
Module System (Project Jigsaw).
As of Java 9, you will also install your package into one of the core JavaFX modules, probably javafx.
media or javafx.graphics. As you will see in the final section in this chapter, using the public and private
keywords correctly will allow you to fully leverage the power of Java 9’s new module features. We’ll be
covering modules in detail at the end of this chapter, after we cover all of the other core Java programming
language features that have existed in many of the previous versions of Java and that are still in use today in
Java 6 (32-bit Android), Java 7 (64-bit Android 5 through 6), and Java 8 (64-bit Android 7 through 8 and the
current version of Java, until Java 9 is released, during the last quarter of 2017).
If you were developing game engines for other game developers to use, however, you would most
probably end up using more of the other three access control modifier keywords that we have been
discussing in this section so that you would be able to control precisely how others would implement your
game engine’s Java code structures. Next, let’s take a look at the nonaccess control modifier keywords, which
are even more intellectually challenging!


Non Access Control Modifiers: Final, Static, and Abstract


The Java modifier keywords that do not specifically provide access control features to your Java constructs
are termed nonaccess control modifier keywords. These include the often used static, final, and abstract
modifier keywords, as well as the not so often used synchronized and volatile modifier keywords, which
are used for more advanced thread control, which I will be covering later during this professional-level
programming title. I will cover those keywords in this section so that you will know what they mean if you
encounter them in your Java programming before then.

Free download pdf