Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


I will cover these concepts in order of their complexity, from the easiest for developers to wrap their
minds around to the most difficult for the object-oriented programming developers to wrap their minds
around. OOP is like surfing, in that it seems very difficult until such time as you have practiced doing it a
large number of times and then suddenly one day you just get it.


Java Final Modifier: Variable Reference, Method, or Class Cannot Be Modified


We have already looked at the final modifier keyword, as it is used to declare a constant along with a static
keyword. A final data field variable can be initialized (set) one time. A final reference variable, which
is a special type of Java variable that contains a reference to an object in memory, cannot be changed
(reassigned) to refer to a different object. The data that is held inside of the (final) referenced object can be
changed, however, as only the reference to the object itself is the final reference variable, which is essentially
“locked in” using a Java final keyword.
A Java method can also be “locked” using the final modifier keyword. When a Java method is made
“final,” it means that if the Java class that contains that method is subclassed, that final method cannot
be overridden, or modified, within the body of the subclass. This essentially “locks” what is inside of the
method code structure. For example, if you wanted the .start() method for your JavaFXGame class (were it to
ever be subclassed) to always do the same things that it does for your JavaFXGame superclass (prepare the
JavaFX staging environment), you would do this:


public class JavaFXGame extends Application {
Button btn;
@Override
public final void start(Stage primaryStage) {
btn = new Button(); // other Java statements can be added
}
}


This would prevent any subclasses (public class JavaFXGame3D extends JavaFXGame) from changing
anything regarding how the JavaFXGame game engine (JavaFX) is set up initially, which is what the .start()
method does for your game applications, as you’ll see in Chapters 7 and 8 , covering the JavaFX 9 multimedia
engine. A class that is declared using a final modifier keyword can’t be extended (also called subclassed),
locking that class against any future usage.


Java Static Modifier: Variables or Methods That Exist Independently of


Instances


As you have seen already, the static keyword can be used in conjunction with the final keyword to
create a constant. The static keyword is used to create Java constructs (methods or variables) that exist
independently or “outside of ” any object instances that are created using the class that static variables or
static methods are defined in. A static variable in a class will force all instances of the class to share the
data in that variable. In other programming languages, this is generally referred to as a global variable, one
everything created by the code can access and share.
Similarly, a static method will also exist outside of instanced objects for that class and will be shared
by all of those objects. A static method will not reference variables “outside of ” itself, such as an instanced
object’s variables.
Generally, the static method will reference its local, or static, variables and constants from its declaring
class and will also take in variables using that method’s parameter list. It will then provide processing or
computation based on those parameters, as well as using the method’s own static or local constants or
variables, and programming logic.

Free download pdf