Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


Constuctor Methods: Turning a Java Class into a Java Object


There is one specialized type of Java method I’m going to cover in detail in this section of this chapter called
a constructor method. This is a special type of method that can be used to create (construct) Java objects,
which we will be covering a bit later in the chapter, after we cover all of the different types of Java syntax
and programming structures that can be used to create, define, and interface with these Java objects. Java
objects just happen to be the foundation of Object-Oriented Programming (OOP), so we will be taking a
look at constructor methods here; it is important to have an understanding of this before we cover the Java
object itself later in the chapter. Since we are covering methods in this section, this is the most logical place
to take a look at constructors, as constructor methods are sometimes called (for short) by veteran Java game
developers, which you are on your way to becoming.


Creating a Java Object: Invoking the Class Constructor Method


A Java class can contain a constructor method with the same name as the class and can be used to create
Java objects using that class. A constructor method uses its Java class as a blueprint to create an instance
of that class in system memory, which creates the Java object. A constructor method will always return a
Java object and thus does not use any of the other Java return types that other methods will typically use
(void, String, float, int, byte, etc.). We will be covering these Java return types later during the chapter. The
constructor method should be invoked by using the Java new keyword since you are creating a new Java
object.
You can see an example of this in the bootstrap JavaFX code shown in Figure 5-2, in line numbers 20, 28,
and 30. These lines are where the Button, StackPane, and Scene objects are created, respectively, by using
the following object declaration, naming, and creation Java code structure, as follows:


=
new

The reason that a Java object is declared in this fashion—using the class name, the name of the object
you’re constructing, the Java new keyword, and the class’s constructor method name (and parameters, if
any) in a single Java statement terminated (finished) with a semicolon character—is because each Java
object is an instance of a Java class.
To use the Button object creation from line 20 of your current Java code as an example, what you
are telling the Java language compiler using the part of the Java statement on the left side of the equals
“operator” is that you want to create a Button type object named btn using a JavaFX Button class as the
object blueprint. This “declares” the Button class (object type) and gives it a unique name. (We will soon be
covering operators, a bit later on in the chapter.)
The first part of creating the object is thus called the object declaration. The second part of creating
your Java object is called the object instantiation, and this part of the object creation process can be seen on
the right side of the equals operator and involves a constructor method and the Java new keyword.
What you do to instantiate a Java object is that you invoke, or utilize, the Java new keyword in
conjunction with an object constructor method call. Since this takes place on the right side of the equals
operator, the result of the object instantiation is placed into the declared object, which is on the left side of
the Java statement. As you will see a bit later in the chapter when we discuss operators, this is what an equals
operator does, and a useful operator it is.
This completes a process of declaring (class name), naming (object name), creating (using a new
keyword), configuring (using a constructor method), and loading (using the equals operator) your very own
custom Java object.