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 />

Creating a <strong>Java</strong> Object: Invoking a Class’s Constructor Method<br />

A <strong>Java</strong> class can contain a constructor method with the exact same name as the class that can be used to create <strong>Java</strong><br />

objects using that class. A constructor method uses its <strong>Java</strong> class like a blueprint to create an instance of that class in<br />

memory, which creates a <strong>Java</strong> object. A constructor method will always return a <strong>Java</strong> object and thus does not use any<br />

of the <strong>Java</strong> return types that other methods will typically use (void, String, and so on). A constructor method is invoked<br />

by using the <strong>Java</strong> new keyword, because you are creating a new object.<br />

You can see an example of this in the bootstrap <strong>Java</strong>FX code shown in Figure 3-2 (ll. 20, 28, and 30), where new<br />

Button, StackPane, and Scene objects are created, respectively, by using the following object declaration, naming,<br />

and creation <strong>Java</strong> code structure:<br />

= new <br />

The reason that a <strong>Java</strong> object is declared in this way, using the class name, the name of the object you are<br />

constructing, the <strong>Java</strong> new keyword, and the class’s constructor method name (and parameters, if any) in a single<br />

<strong>Java</strong> statement terminated with a semicolon, is because a <strong>Java</strong> object is an instance of a <strong>Java</strong> class.<br />

Let’s take a look at, for example, the Button object creation from line 20 of your current <strong>Java</strong> code. Here, via the<br />

part of the <strong>Java</strong> statement on the left-hand side of the equals operator, you are telling the <strong>Java</strong> language compiler that<br />

you want to create a Button object named btn, using the <strong>Java</strong>FX Button class as the object blueprint. This declares the<br />

Button class (object type) and gives it a unique name.<br />

The first part of creating the object is thus called the object declaration. The second part of creating your <strong>Java</strong><br />

object is called the object instantiation, and this part of the object creation process, seen on the right-hand side of the<br />

equals operator, involves a constructor method and the <strong>Java</strong> new keyword.<br />

To instantiate a <strong>Java</strong> object, you invoke the <strong>Java</strong> new keyword, in conjunction with an object constructor method<br />

call. Because this takes place on the right-hand side of the equals operator, the result of the object instantiation is<br />

placed in the declared object, which is on the left-hand side of the <strong>Java</strong> statement. As you will see a bit later in the<br />

chapter, when I discuss operators (see the section “<strong>Java</strong> Operators: Manipulating Data in the Application”), this is<br />

what an equals operator does, and a useful operator it is.<br />

This completes the process of declaring (class name), naming (object name), creating (using a new keyword),<br />

configuring (using a constructor method), and loading (using the equals operator) your very own custom <strong>Java</strong> object.<br />

It is important to note that the declaration and instancing parts of this process can be coded using separate lines<br />

of <strong>Java</strong> code as well. For instance, the Button object instantiation (see Figure 3-2, l. 20) could be coded as follows:<br />

Button btn;<br />

btn = new Button();<br />

This is significant, because coding an object creation in this way allows you to declare an object at the top of your<br />

class, where each of the methods inside the class that use or access these objects can see the object. In <strong>Java</strong>, unless<br />

declared otherwise, using modifiers, an object or data field is only visible inside the <strong>Java</strong> programming construct<br />

(class or method) in which it is declared.<br />

If you declare an object inside your class, and therefore outside all the methods contained in the class, then all<br />

the methods in your class can access (use) that object. Similarly, anything declared inside a method is local to that<br />

method and is only visible to other members of that method (<strong>Java</strong> statements inside the method scope delimiters). If<br />

you wanted to implement this separate object declaration (in the class, outside the methods) and object instantiation<br />

(inside the .start() method) in your current InvinciBagel class, the first few lines of <strong>Java</strong> code for your InvinciBagel<br />

class would change to look like the following <strong>Java</strong> programming logic:<br />

public class InvinciBagel extends Application {<br />

Button btn;<br />

@Override<br />

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

btn = new Button();<br />

www.it-ebooks.info<br />

53

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

Saved successfully!

Ooh no, something went wrong!