Learn Object-Oriented Java the Hard Way

Exercise 12: Reviewing Constructors

There’s nothing really new in this exercise, so if you’re totally comfortable with constructors and private instance variables, then feel free to skip this one.

But constructors are very important, so I want to cover them one more time before moving on to a new topic.

Rectangle.java
 1 public class Rectangle {
 2     private int length, width;
 3 
 4     public Rectangle() {
 5         length = width = 0;
 6     }
 7 
 8     public Rectangle( int l, int w ) {
 9         length = l;
10         width = w;
11     }
12 
13     public int getArea() {
14         return length*width;
15     }
16 }

This (boring) Rectangle class has two private fields. (In the future, I probably won’t bother to write “private fields”; calling them “fields” pretty much implies that they will be private. That’s just how Java programmers do things. I probably should have written getters and setters for them, but this exercise is just focused on constructors so I left them out.)

There are two constructors. There are three things you should remember about constructors.

  1. They have the same name as the class (Rectangle in this case).
  2. They do not have a return type – not even “void”.
  3. Their “job” is to make sure all necessary setup has been done. This means initializing all the instance variables, but sometimes other stuff happens, too.

I was going to add “Constructors must be public,” but that’s not true. They are usually public but sometimes you want a constructor but don’t want people to be able to call it, so you make one of the constructors private or something.

RectangleDriver.java
 1 public class RectangleDriver {
 2     public static void main( String[] args ) {
 3         // Rectangle r = new Rectangle();
 4         // r.length = 10;
 5         // r.width = 5;
 6 
 7         Rectangle r = new Rectangle(10, 5);
 8         System.out.println("The area is " + r.getArea());
 9     }
10 }

Lines 3-5 in the driver are commented out, but they show what you would have done to instantiate the object several exercises ago. You must construct the object itself (line 3), and then put values into all the fields.

Now that our fields are private, and now that we have constructors, we can accomplish all this in just a single line in the driver. This is shown on line 7.

The first thing that happens on line 7 is the left-hand side of the equal sign. The compiler creates a Rectangle object and names it r. At first, it doesn’t have an object in it.

Then the right-hand side of the equal sign is done: it calls the parameter constructor, passing in 10 and 5 as parameters. Because the 10 is first, a copy gets put into the first parameter (the int l). Then a copy of the 5 gets passed into the second parameter (the int named w).

Secretly behind the scenes just before line 9 our Rectangle object is actually instantiated in memory. Then on lines 9 and 10 the constructor copies the values from the parameters into the instance variables.

Once the constructor ends on line 11 one other thing happens behind the scenes that *doesn’t happen in regular methods: a reference to the Rectangle object is returned back to the driver. (We’ll learn more about this in a later exercise.)

This sends us back to line 7 of the driver, where the right-hand side has just completed. So finally the “equal sign” part of the line happens; (a reference to) the object returned from the constructor gets stored into the variable on the left hand side (r).

So at this point line 7 is completely done, and the object has been instantiated and the fields have values.

That’s a lot of doing for one line of code, eh? That’s why constructors are nice, actually. It’s a little more complicated than doing all those things manually, but it’s nicer for the person using our class and it’s safer since it makes certain all that setup has been completed before the object gets used.

What You Should See

Cool?


“Learn Object-Oriented Java the Hard Way” is ©2015–2016 Graham Mitchell