<< Chapter < Page | Chapter >> Page > |
Displayed in the center of the world
When the two Turtle objects instantiated in Listing 2 come into existence, they will be displayed in the center of the World object referred to by the contents of the variable named mars . However, that happens so quickly that you probably won't see it when you run this program.
Eliminating the run method call
If you were to eliminate the call to the run method in Listing 1 , you would see a world with a white background and a single turtle positioned in the center of the world facing north. There would actually be two turtles there, but they would be in exactly the same location so only the one closest to you would be visible.
The constructor for the Prob01Runner class
That's probably enough discussion of the three statements in Listing 2 . The constructor for the class named Prob01Runner is shown in its entirety in Listing 3 .
Listing 3 . The constructor for the Prob01Runner class. |
---|
public Prob01Runner(){//constructor
System.out.println("Dick Baldwin.");}//end constructor |
The purpose of constructors
The primary purpose for which constructors exist is to assist in the initialization of the variables belonging to the object being constructed. However, it is possible to directly initialize the variables as shown in Listing 2 .
Initialization of variables
When an object comes into existence, the variables belonging to that object will have been initialized by any direct initializers like those shown in Listing 2 as well any initialization produced by code written into the constructor.
Default initialization
If a variable (exclusive of local variables inside of methods) is not initialized in one of those two ways, it will receive a default initialization value. The default values are:
Non-initialization code in constructors
Although it is usually not good programming practice to do so, there is no technical reason that you can't write code into the constructor that has nothing to do with variable initialization. Such code will be executed when the object is instantiated.
An object counter
For example, you might need to keep track of the number of objects that are instantiated from a particular class, such as the total number of asteroid objects in a game program for example You could write the code to do the counting in the constructor.
Display my name
The code in the constructor in Listing 3 simply causes my name to be displayed on the command-line screen when the object is instantiated. That is how my name appears ahead of the other lines of output text in Figure 2 . My name is displayed when the object is instantiated. The remaining three lines of text in Figure 2 are displayed later by manipulating the object.
Three accessor methods
Listing 4 defines three accessor methods that are used to access and return copies of the contents of the private instance variables named joe , sue , and mars .
Listing 4 . Three accessor methods. |
---|
public Turtle getJoe(){return joe;}
public Turtle getSue(){return sue;}public World getMars(){return mars;} |
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?