Game Development Reference
In-Depth Information
If you wanted to implement this separate object declaration (in the class, outside the
methods) and object instantiation (inside the .start() method) in your current Invin-
ciBagel class, the first few lines of Java code for your InvinciBagel class would change
to look like the following Java programming logic:
public class InvinciBagel extends Application {
Button btn;
@Override
public void start(Stage primaryStage) {
btn = new Button();
btn.setText("Say 'Hello World'");
// The other programming statements continue
underneath here
}
}
When the object declaration and instantiation are split up, they can be placed inside
(or outside) methods as needed for visibility. In the preceding code, other methods of
your InvinciBagel class could call the .setText() method call shown without the Java
compiler's throwing an error. The way the Button object is declared in Figure 3-2 , only
the .start() method can see the object, and so only the .start() method can use this
btn.setText() method call.
Creating a Constructor Method: Coding an Object's
Structure
A constructor method is more a method for creating objects in system memory, where-
as other methods (or functions, if using a different programming language) are usually
used to perform calculation or processing of one type or another. The constructor meth-
od's use in creating Java objects in memory, rather than performing some other pro-
gramming function, is evidenced by the use of the Java new keyword, which creates a
new object in memory. For this reason, a constructor method will define the structure
of an object as well as allow the calling entity to populate the object structure with
custom data values, using the constructor method's parameter list .
You will create a couple of sample constructor methods in this section to learn the
basics of how this is done as well as what a constructor method usually contains. Let's
Search WWH ::




Custom Search