28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

<strong>Java</strong>’s final Modifier: Variables, Methods, and Classes That Cannot Be Modified<br />

You have already explored the final modifier keyword as it is used to declare a constant, along with a static keyword.<br />

A final data field variable can only be initialized (set) one time. A final reference variable, which is a special type of<br />

<strong>Java</strong> variable that contains a reference to an object in memory, cannot be changed (reassigned) to refer to a different<br />

object; the data that are held inside the (final) referenced object can be changed, however, as only the reference to the<br />

object itself is the final reference variable, which is essentially locked in, using a <strong>Java</strong> final keyword.<br />

A <strong>Java</strong> method can also be locked using the final modifier keyword. When a <strong>Java</strong> method is made final, if the <strong>Java</strong><br />

class that contains that method is subclassed, that final method cannot be overridden, or modified, within the body<br />

of the subclass. This basically locks what is inside the method code structure. For example, if you want the .start()<br />

method for your InvinciBagel class (were it ever to be subclassed) always to do the same things that it does for your<br />

InvinciBagel superclass (prepare a <strong>Java</strong>FX staging environment), you use the following code:<br />

public class InvinciBagel extends Application {<br />

Button btn;<br />

}<br />

@Override<br />

public final void start(Stage primaryStage) {<br />

btn = new Button();<br />

// The other method programming statements continue here<br />

}<br />

This prevents any subclasses (public class InvinciBagelReturns extends InvinciBagel) from changing anything<br />

regarding how the InvinciBagel game engine (<strong>Java</strong>FX) is set up initially, which is what the .start() method does for<br />

your game application (see Chapter 4). A class that is declared using a final modifier keyword cannot be extended, or<br />

subclassed, locking that class for future use.<br />

<strong>Java</strong>’s Static Modifier: Variables or Methods That Exist in a Class (Not in Objects)<br />

As you have already seen, the static keyword can be used in conjunction with the final keyword to create a constant.<br />

The static keyword is used to create <strong>Java</strong> constructs (methods or variables) that exist independently, or outside, any<br />

object instances that are created using the class that static variables or static methods are defined in. A static variable<br />

in a class will force all instances of the class to share the data in that variable, almost as if it is a global variable as far as<br />

objects created from that class are concerned. Similarly, a static method will also exist outside instanced objects for<br />

that class and will be shared by all those objects. A static method will not reference variables outside itself, such as an<br />

instanced object’s variables.<br />

Generally, a static method will have its own internal (local or static) variables and constants and will also take in<br />

variables, using the method parameter list, and then provide processing and computation, based on those parameters<br />

and its own internal (static local) constants if needed. Because static is a concept that applies to instances of a class,<br />

and is thus at a lower level than any class itself, a class would not be declared using a static modifier keyword.<br />

<strong>Java</strong>’s Abstract Modifier: Classes and Methods to Be Extended and Implemented<br />

The <strong>Java</strong> abstract modifier keyword has more to do with protecting your actual code than with code that has been<br />

placed in memory (object instances and variables, and so on) at runtime. The abstract keyword allows you to specify<br />

how the code will be used as a superclass, that is, how it is implemented in a subclass once it is extended. For this<br />

reason, it applies only to classes and methods and not to data fields (variables and constants).<br />

www.it-ebooks.info<br />

59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!