Core Java · Chapter 11 of 48
Constructors and this
By the end of this chapter you can make an object ready the moment it is built, with constructors that share their work through this(...), and use this to reach a field or to return the object itself. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Objects
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Ravi rings from New Delhi. "We opened three counters at NDLS this morning and one of them shows null / null: 0 cups left." Somebody built the counter and forgot to set its fields. He also wants each morning's counter to open with whatever was left from the night before.
In the last chapter you built an object with new and then set its fields one line at a time. Nothing forced anyone to write those lines: skip one and the object exists half-empty, and the mistake shows up much later, on a screen at a station.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
StationCounter.java: fix the constructor whose parameters hide the fields
In StationCounter.java, fix the full constructor StationCounter(station, name, openingStock) so it stores its three parameters in the counter's station, name and openingStock fields. Each of its lines now assigns a parameter to itself, so the fields stay null and 0. A new counter has sold nothing, so its cups sold is 0 and its stock left equals its opening stock.
For example, new StationCounter("SBC", "Platform 1", 120) has station SBC, name Platform 1, opening stock 120, 0 cups sold and 120 cups left.
- Task 2
StationCounter.java: chain the shorter constructors to the full one
In StationCounter.java, write the two shorter constructors by calling another constructor with this(...), not by setting the fields again. StationCounter(station, openingStock) makes a counter named Main counter. StationCounter(station) makes a counter named Main counter with the default stock of 100 cups.
For example, new StationCounter("NDLS", 80) has station NDLS, name Main counter and opening stock 80; new StationCounter("CSMT") has station CSMT, name Main counter and opening stock 100.
- Task 3
StationCounter.java: open today's counter from yesterday's
In StationCounter.java, write the handover constructor StationCounter(yesterday). It builds today's counter, a new object, with yesterday's station and name, and an opening stock equal to the cups yesterday's counter had left. Nothing is sold yet today, and yesterday's counter must not change.
For example, if yesterday's SBC / Platform 1 counter opened with 120 and sold 90, today's counter is SBC / Platform 1 with opening stock 30 and 0 cups sold, and yesterday's still has 90 sold.
- Task 4
StationCounter.java: return this from sell so sales can be chained
In StationCounter.java, make sell(cups) add cups to this counter's cups sold and then return this same counter object, never a new one and never null, so calls can chain one after another.
For example, after counter.sell(3) returns the counter itself, counter.sell(2).sell(5) leaves cups sold at 10, and a counter that opened with 120 then has 110 left.
- Task 5
StationCounter.java: remove the constructor that makes a counter with no station
In StationCounter.java, delete the constructor that takes no arguments, so a counter can no longer be built without a station. Keep the other four constructors: the check counts exactly four, and none of them takes no arguments.
For example, after the change new StationCounter("SBC") still compiles, and new StationCounter() with no arguments does not.
What you will learn
- Constructors: code that runs when new builds an object, so it starts complete.
- this: a reference to the object being built or used, to reach a field a parameter hides, or to return the object.
- this(...): one constructor handing its work to another.
- The default constructor: the free one Java gives a class, and when it disappears.
More in Objects
Build "Constructors and this" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.