Game Development Reference
In-Depth Information
method name, but different parameter list configurations. What this means is that, if
you have defined more than one method with the same name, Java can figure out
which of your (overloaded) methods to use by looking at the parameters that are being
passed into the method being called and then using that parameter list to discern which
of the methods (that have the same name) to use by matching the parameter list data
types and names and the order in which they appear. Of course, your parameter list
configurations must all be unique for this Java method overloading feature to work cor-
rectly.
You will be learning how to use and how to code Java methods over the course of
this topic, beginning in Chapter 4 , so I am not going to spend too much time on them
here, other than to define what they are and the basic rules for how they are declared,
and used, inside Java classes.
One specialized kind of method that I am going to cover in detail, however, is the
constructor method . This is a type of method that can be used to create objects. Java
objects are the foundation of OOP, so you will be taking a look at constructor methods
next, as it is important to do so before learning about the Java object itself, which you
will study later in the chapter (see the section “Java Objects: Virtual Reality, Using
Java Constructs“).
Creating a Java Object: Invoking a Class's Constructor
Method
A Java class can contain a constructor method with the exact same name as the class
that can be used to create Java objects using that class. A constructor method uses its
Java class like a blueprint to create an instance of that class in memory, which creates
a Java object. A constructor method will always return a Java object and thus does not
use any of the Java return types that other methods will typically use (void, String, and
so on). A constructor method is invoked by using the Java new keyword , because you
are creating a new object.
You can see an example of this in the bootstrap JavaFX code shown in Figure 3-2
(ll. 20, 28, and 30), where new Button, StackPane, and Scene objects are created, re-
spectively, by using the following object declaration, naming, and creation Java code
structure:
<Java class name> <your object instance name> = new <Java
constructor method name> <semicolon>
Search WWH ::




Custom Search