Pro Java 9 Games Development Leveraging the JavaFX APIs

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

Now that you understand the primary Java programming logic constructs or structures, you’re ready to
learn about (or review) more complicated language features, such as modifiers, operators, data types, and
statements.


Java Modifier Keywords: Access Control and More


Java modifier keywords are reserved Java keywords that modify the access control, visibility, or longevity
(how long something exists in memory, during the execution of your application) for code or data structures
inside of the primary types of Java programming structures that you have learned about (reviewed) thus far.
The modifier keywords are the first Java reserved words that are “declared” or utilized on the “outside” and
“head” (beginning) of your Java code structures since the Java logic for the structure, at least for classes and
methods, is contained within the curly braces {...} delimiters, which come after the class keyword and class
name or after the method name and parameter list. Modifier keywords come before any of these and can be
utilized with your Java classes, methods, data fields (variables and constants), and Java interfaces, which we
will be covering a bit later.
As you can see at the bottom of Figure 5-2 for the .main() method, which was created by NetBeans 9 for
the BoardGame class definition (which uses the public modifier that we are going to be covering next), you
can use more than one Java modifier keyword. The .main() method first uses a public modifier keyword,
which is the access control modifier keyword, and then it uses a static modifier keyword second, which is a
nonaccess control modifier keyword. Let’s cover the Java access control modifiers next, and after that, we
will get into the much more complex nonaccess control modifiers. These access control modifiers become
much more important with the extra security protection in Java 9 afforded by the Java Modules feature,
which controls how your packages and API are bundled and distributed.


Access Control Modifiers: Public, Protected, Package, or Private


Let’s cover access control modifiers first since they are declared first before any nonaccess control modifier
keywords and before any return type keywords; they are easier to understand conceptually as well. There
are four access control modifier levels that can be applied to any Java programming structure. If you do
not declare any access control modifier keyword, a “default” access control level of package private
will be applied to that Java code structure, which allows it to be “visible to,” and thus usable by, any Java
programming structure inside of your Java package. In this case, that would be the boardgame package.
The other three Java access control modification levels all have their own access control modifier
keywords, including the public, private, and protected keywords. These are aptly named for what they do,
so you probably have a fairly good idea of how to apply these to either share your code publicly or protect it
from public usage already, but let’s cover each of these in detail here, just to make sure. As you know, access
control, as in security, is the important issue for Java software these days, both inside of your code and in
the outside world, which is why Java 9 added modules. We will start with the least amount of access control
(security) first, with the public access control modifier.


Java Public Modifier: Variables or Methods That Exist Independently of


Instances


The Java public access modifier keyword can be used by classes, methods, constructors, data fields
(variables and constants), and interfaces. If you declare something as public, it can be accessed by the
public. This means it can be imported and utilized by any other class, in any other package, as long as it is
exported in a module. Essentially this means your code can be used in any software that is created using the
Java 9 language. As you will see in the classes that you use from the Java and JavaFX programming platforms

Free download pdf