Pro Java 9 Games Development Leveraging the JavaFX APIs

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

Since static is a concept that applies to instances of a class and is thus inherently at a lower level that
any class itself, a Java class would therefore not ever be declared using the static nonaccess control
modifier keyword.


Java Abstract Modifier: Classes or Methods to Be Extended or Implemented


The Java abstract modifier keyword has more to do with protecting your actual code than it has to do with
code that has been placed into memory (object instances and variables and so on) at run time. The abstract
keyword allows you to specify how the code will be utilized as a superclass, that is, how it is implemented in
a subclass once it is extended. For this reason, the abstract modifier keyword would only apply to classes and
methods and would not apply to data fields (variables and constants), as these data structures hold values
and are not code (programming logic) constructs.
A class that has been declared using the abstract modifier keyword cannot be instanced, and it is
intended to be used only as a superclass (blueprint) to create (extend) other classes. Since a final class
cannot be extended, you will not use the final and the abstract modifier keywords together, at a class level.
If a class contains any method that has been declared using the abstract modifier keyword, that class
must then itself be declared to be an abstract class. An abstract class does not have to contain any abstract
methods, however.
A method that has been declared using the abstract modifier keyword is a method that has been
declared for use in subclasses but that has no current implementation. This means it will have zero Java
code inside of its “method body,” which, as you know, is delineated in Java by using the curly braces. Any
subclass that extends an abstract class must implement all of these abstract methods, unless that subclass
is also subsequently declared to be abstract, in which case the abstract method is passed down to the next
subclass level to eventually be implemented.


Java Volatile Modifier: Advanced Multithreading Control Over Your Data Fields


The Java volatile modifier keyword is used when you are developing multithreaded applications, which
you are not going to be doing for Java 9 game development, as you want to optimize your game well enough
so that it only uses the JavaFX threads. What the volatile modifier does is to tell the Java Virtual Machine
(JVM) that is running your application to merge the private (that thread’s) copy of the data field (variable or
constant) that has been declared as volatile with the master copy of that variable in system memory.
Volatility is associated with the property of visibility to the running app. When a variable is declared
volatile, a write will affect the main memory copy of a variable so that any thread running on any CPU or
core will observe the change. When a variable is not declared to be volatile, that write is made to a cached
copy, so only the thread making that change will be able to observe that change. Only use volatile when it’s
absolutely necessary for your Java 9 game.
This is similar to the static modifier keyword, with the difference that a static variable (data field) is
shared by more than one object instance, whereas a volatile data field (variable or constant) is shared by
more than one thread.


Java Synchronized Modifier: Advanced Multithreading Control Over Methods


The Java synchronized modifier keyword is also used when you’re developing multithreaded applications,
which we are not going to be doing for your Java 9 game development engine in this particular book. What
the synchronized modifier does is to tell the Java Virtual Machine (JVM) that is running your application
that the method that has been declared as synchronized can be accessed by only one thread at a time.
This concept is similar to the concept of synchronized in database access, so you don’t have data record
access collisions. A synchronized modifier keyword thus also prevents these collisions between threads

Free download pdf