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.
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.
Rectangle
in this case).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.
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.
Cool?
“Learn Object-Oriented Java the Hard Way” is ©2015–2016 Graham Mitchell