Game Development Reference
In-Depth Information
getter and setter methods in place for classes and objects external to InvinciBagel to re-
quest that data, we will need to modify the Bagel class constructor method to receive a
copy of the InvinciBagel object so that the calling class has “digital context” as to what
the InvinciBagel class (and thus object) has to offer. This is done using an additional
parameter, the Java this keyword, in the Bagel() parameter list.
Passing Context from InvinciBagel to Bagel: Using
this Keyword
The final piece in the puzzle regarding how to eliminate static import statements, and
reach between classes (objects) in a legitimate fashion, is to pass the InvinciBagel
class's current configuration, held in a contextual object reference, which the this
keyword actually represents, over to the Bagel class (object) using the Bagel() con-
structor method. Once the Bagel class has received this contextual information re-
garding how the InvinciBagel class (object) is set up, what it includes, and what it does
(hey, I have not called this object reference a “contextual” object reference for no reas-
on), it will be able to use the .isUp() method to “see” the value of a Boolean up vari-
able, without having any static declaration in place anywhere other than for constants,
which is what an import static reference should be used for.
The first thing that we need to do to upgrade your Bagel class is to set up a variable
to hold this InvinciBagel contextual object reference information, and modify our cur-
rent Bagel() constructor method so that it can receive an InvinciBagel object reference.
We'll need to add a protected InvinciBagel invinciBagel; statement, at
the top of the class, to create an invinciBagel reference object (the variable will hold a
reference to this object in memory) to hold this information. The reason I am making
this protected access is so that if we make any subclass using Bagel, it will have ac-
cess to this contextual object reference information. This object declaration would use
the following Java statement, located at the very top of the Bagel.java class, as shown
in Figure 12-4 :
protected InvinciBagel invinciBagel ;
 
Search WWH ::




Custom Search