Page 300 - Introduction to Programming with Java: A Problem Solving Approach
P. 300
266 Chapter 7 Object-Oriented Programming—Additional Details
This code fragment uses one statement to allocate space for a new object, and it uses another statement to initialize that object’s instance variables. Since the instantiation and initialization of an object is so com- mon, wouldn’t it be nice if there were a single statement that could handle both of these operations? There is such a statement, and here it is:
Car allexCar = new Car("Porsche", 2006, "beige");
This unifies the creation of an object and the initialization of its instance variables in just one call. It guar- antees that an object’s instance variables are initialized as soon as the object is created. The code that follows the word new should remind you of a method call. Both that code and a method call consist of a programmer-defined word (Car in this case) and then parentheses around a list of items. You can think of that code as a special method call, but it’s so special that it has its own name. It’s used to construct objects, so it’s called a constructor.
What Is a Constructor?
A constructor is a method-like entity that’s called automatically when an object is instantiated. The above new Car("Porsche", 2006, "beige")objectinstantiationcallsaconstructornamedCarthat has three parameters—a String, an int, and a String. Here’s an example of such a constructor:
}
public Car(String m, int y, String c)
{
this.make = m;
this.year = y;
this.color = c;
Apago PDF Enhancer
As you can see, this constructor simply assigns passed-in parameter values to their corresponding instance variables. After the constructor is executed, the JVM returns the address of the newly instantiated and initializedobjecttotheplacewheretheconstructorwascalled.IntheaboveCar allexCar = new Car("Porsche", 2006, "beige")declaration,theaddressoftheinstantiatedCarobjectgetsas- signed to the allexCar reference variable.
There are several constructor details you should know before looking at a complete program example. A constructor’s name must be the same as the class it’s associated with. Thus, a Car class’s constructor must be named Car, with an uppercase “C.”
In the heading of a method, you must include a return type, so you might expect the same require- ment for the heading of a constructor. Nope. Return types are not used in constructor headings2 because a constructor call (with new) automatically returns a reference to the object it constructs, and the type of this object is always specified by the constructor name itself. Just specify public at the left and then write the class name (which is the name of the constructor).
An Example
Let’s now look at a complete program example that uses a constructor. See the Car4 program in Figures 7.13 and 7.14. In Figure 7.13, note that we put the constructor above the getMake method. In all class defini- tions, it’s good style to put constructors above methods.
2 If you try to define a constructor with a return type specification, the compiler will not recognize it as a constructor and will think it is an ordinary method instead.