Pro Java 9 Games Development Leveraging the JavaFX APIs

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

It’s important to note that the declaration and instantiation part of this process can be coded using
separate lines of Java code. For instance, the Button object instantiation (Figure 5-2, line 20) could be coded
as follows:


Button btn; // Declare a Button object named btn
btn = new Button(); // Instantiate btn object using Java new keyword and Button()
constructor


The reason this is significant is because coding an object creation in this way allows you to declare an
object at the top of your class, where each of the methods inside of the class that use or access these objects
can “see” the object. In Java, unless declared as otherwise using modifiers, which we will be covering next,
an object or data field is only visible inside of the Java programming construct (class or method) that it is
declared inside of.
If you declare an object inside of your class, and therefore outside of all the methods contained in the
class, all of the methods in your class can access (see and use) that object. Similarly, anything declared
inside of a method is “local” to that method and is only “visible” to other “members” of that method,
meaning all Java statements inside of that method scope that is inside of the {...} delimiters. If you wanted
to implement this separate object declaration in the class, outside of the methods and object instantiation
inside of the .start() method, in a current BoardGame class, the first few lines of Java code for your class
would change to look like the following Java programming logic:


public class BoardGame extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
// other programming statements continue here
}
}


When the object declaration and instantiation are split up, they can be placed inside (or outside) of
methods as needed for visibility. In the previous code, other methods of the BoardGame class could call a
btn.setText() method call shown earlier without the Java compiler “throwing” an error. The way the Button
object is declared in Figure 5-2, only the .start() method can “see” the object, so only the .start() method can
implement the btn.setText() method call.


Creating a Constructor Method: Designing and Coding a Java Object


Structure


A constructor method is a specialized type of method that is utilized to create an object in system memory.
This differs significantly from other methods (if you use a different programming language, you are used to
referring to them as functions). Nonconstructor methods in Java are used to perform some sort of complex
calculation or encapsulated (modularized) processing of one form or another. The constructor method’s
usage for creating Java objects in memory, rather than performing some other programming functionality,
as evidenced by the use of the Java new keyword in conjunction with the constructor method, which creates
a new Java object of that unique class type in memory. For this reason, a constructor method will therefore
define an internal structure of a unique type of Java object. If you want to configure the Java object at the
same time as you instantiate it, you can define the constructor method parameter list to allow the calling
entity to populate the object structure with specific (custom) data values. That way, you can create different
types of that object by passing in different attributes in the constructor’s parameter list.

Free download pdf