Overview
Topic 3.4 is the first time students write a constructor. In Topic 1.13 they only called constructors someone else wrote; in Topic 3.3 they saw the parts of a class. Day 1 covers an object’s state and the has-a relationship (3.4.A.1), what a constructor is and what happens in memory when new calls it, and how parameters supply starting values (3.4.A.2). Day 2 covers default values and the default constructor (3.4.A.4, 3.4.A.5), and why a constructor should store a copy of a mutable object it receives (3.4.A.3).
Run one habit through both days: a constructor gives every instance variable a starting value, on purpose. Each mistake in this topic (a skipped variable, a same-name parameter, a backward assignment, a new local variable) ends the same way: the instance variable keeps its default.
Stay inside the topic boundary. Writing methods is Topic 3.5, so students write only constructors today. Slides 16 and 17 and several worksheet questions include a short void method such as show() that only prints the object’s state; students read it, and you say that writing methods comes next topic. The keyword this is Topic 3.9: every parameter today has a different name from its instance variable. Do not mention this, even when a student asks how to reuse the name; say “Topic 3.9 shows a way.”
Teacher note
The slides are the Starting State deck (22 slides); slide numbers below refer to it. Day 1 uses slides 1–13 (Parts 1 and 2), and Day 2 uses slides 14–22 (Parts 3 and 4, vocabulary, closure). The Starting State puzzle worksheet (10 questions, about 30 minutes) is split across the days: questions 2, 6, and 7 are Day 1 homework; questions 1 and 9 are done in class on Day 2; questions 3, 4, 5, 8, and 10 are Day 2 homework.
The demonstrations are the study guide’s examples, in the topic’s examples/ folder. Each file holds two classes, a public one with main and the class being built, so it runs as one program. Open them in the IDE before class.
- Day 1:
BookCreation,ScoreboardStart,BookVoid(compile error),PetCreation,ConcertTicketPrice,PetSameName. - Day 2:
PlayerForgot,DefaultLamp,NoDefaultAnymore(compile error),GraderShared,OrderCopy. - Printed only (exit ticket):
ExitLantern.
Day 1 · Warm-up: A brand-new bike
Show slide 2. A bike-share app adds a new bike and tracks its ID number, its battery level from 0 to 100, and whether it is rented right now. Partners answer the two questions on the slide: which facts are the same for every new bike, and which one the app must be told? Take answers: every new bike starts fully charged (100.0) and not rented; only the ID is different, so someone has to pass it in. Leave three words on the board, ID, fixed, passed in: by the end of Day 1 students will write the constructor that does exactly this.
Day 1 · Direct instruction: State, has-a, and the parts of a constructor
- Goals (slide 3). Read the six goals. Goals 1–4 are today’s; 5 and 6 are Day 2’s.
- State and has-a (3.4.A.1, slides 4–5). An object’s state is the values of its instance variables at a given moment. Read the table down a column: bike 417 has
100.0andfalse; bike 52 has38.5andtrue. The instance variables come from the class and are the same for every bike; the values differ from object to object and change over time. Then say the has-a sentences aloud: a bike has an ID, has a battery level, has a rented status. Give students the design test: if “a ___ has a ___” sounds right, the second blank is a candidate instance variable. - Anatomy of a constructor (3.4.A.2, slide 6). Walk the callouts top to bottom:
public(always, in this course), exactly the class name (capital included), no return type, not evenvoid, the parameter list, and a body that gives every instance variable a value. Point atbattery = 100.0;andrented = false;: a starting value can be fixed by the constructor instead of coming from a parameter. That answers the warm-up. - What
new Bike(417)does (3.4.A.2, slide 7). Number the three steps on the board: memory is set aside for a new object with its own instance variables; the body runs, withbikeIdholding a copy of 417; a reference is handed back and stored inrental. Ask why417 100.0prints betweenStartandDone: the constructor finishes beforemainmoves on. RunBookCreationin the IDE to show the same order with aBook. - Two objects, two references. Run
ScoreboardStart: two scoreboards with identical states, and==printsfalse. Eachnewsets aside separate memory and returns a different reference (Topic 2.6). Same state does not mean same object. - A return type turns it into a method (slide 8). Show
BookVoidin the IDE and ask students to predict before compiling. Withvoid, the “constructor” is an ordinary method that happens to share the class’s name, so the call that passes arguments fails. The error points at the call, but the mistake is in the header. Do not explain “required: no arguments” yet; tell students Day 2 explains where that no-argument constructor comes from.
Misconception: Mixing up the instance variables with the state
Students say “the state of a book is title, pages, and checkedOut.” Those are the instance variables, which come from the class and are the same for every book. The state is the values in them for one object at one moment. On the slide 5 table, have students point to a variable name (a row label) and then to a state (a column of values).
Misconception: Giving a constructor a return type
Because every method they have seen has a return type, students write public void Book(...). Java then treats it as a method, and the class has no constructor that takes those arguments. BookVoid and slide 8 show the compiler error. Have students check every header they write for one thing: nothing between public and the class name.
Day 1 · Guided practice: Two tickets, one constructor
Slide 9. Students trace on mini whiteboards and write the exact output. The constructor runs once per new: row is 3, 3 <= 5 is true, price is 40.0; then row is 12, the else sets 25.0. Output: 3 40.0 then 12 25.0. Ask two follow-ups: “How many Ticket objects exist?” (two, one per new) and “Where does price get its value?” (computed from the parameter, not passed in). Run ConcertTicketPrice afterward for the same idea with four instance variables.
Day 1 · Direct instruction: Parameters supply the values
- From parameter to instance variable (3.4.A.2, slides 10–11). The caller’s arguments arrive in the parameters as copies (Topic 1.13). In the
Backpackconstructor,nameis copied fromowner,pocketsis fixed at 4, andheavyis computed frompounds. Every instance variable gets a value, by one of those three routes. RunPetCreation: same constructor, different arguments, different starting states. - Store it or lose it. A parameter is a temporary variable that vanishes when the constructor ends; an instance variable lasts as long as the object. Ask: “
Backpackhas no instance variable forpounds. Where is 12.5 after the constructor ends?” Gone, and that is fine here, because onlyheavyneeded it. - Three ways to never set an instance variable (slide 12). Go row by row and ask what
batteryholds afternew Phone(80)each time. All three leave it at 0 (say “the default; Day 2 explains why it is 0”). Then show the correct version: a different parameter name, with the instance variable on the left of=. - Same name, live. Run
PetSameName. It compiles and printsA Pet was created., but the pet never gets its name or age. Inside the constructor the namenamemeans the parameter, soname = name;copies the parameter into itself. Rename the parameterspetNameandpetAge, assignname = petName;, and the bug is gone.
Misconception: Naming a parameter the same as its instance variable
It feels natural to call the parameter name because it fills name. Inside the constructor the closer declaration wins, so both sides of name = name; mean the parameter and the instance variable is never assigned. There is no compiler error, which is why it is dangerous. Rule for now: give each parameter a different name (petName for name). Topic 3.9 shows another way; do not teach it today.
Misconception: Writing the assignment backward
Students write pageCount = pages; because both are “the page count.” = stores the right side into the variable on the left, so this overwrites the parameter and never touches pages. Have students say each assignment aloud as “store ___ into ___,” and check that the instance variable is the one being stored into.
Misconception: Declaring a new local variable by accident
int battery = level; inside a constructor declares a brand-new local variable that disappears when the constructor ends; the instance variable is untouched. Students copy the type from the instance variable declaration out of habit. Point out that the type was already given once, at the top of the class: in a constructor, assignments to instance variables never start with a type.
Day 1 · Guided practice: Which one stores the volume?
Slide 13. Students hold up A, B, C, or D, then defend it to a partner before you reveal. B is correct: a different name, with volume on the left of =. A copies the parameter into itself; C has void, so it is a method, not a constructor; D runs backward and overwrites the parameter. Each wrong choice matches one of the mistakes from slides 8 and 12, so ask “which slide is this the mistake from?”
Day 1 · Independent practice: Independent practice
Worksheet questions 2, 6, and 7 are homework (about 10 minutes). Question 2 is a be-the-JVM == check on two kites with the same state (answer B, false true); question 6 is the one-token fix where int flies = caught; declares a local variable; question 7 is the face-off between an instance variable and a parameter. Each uses only Day 1 material.
Day 1 · Closure: Closure
Students finish this sentence on a sticky note: “A parameter vanishes when the constructor ends, so the constructor must …” Look for “assign it to an instance variable” (or “store it”). Collect a few and read them aloud at the start of Day 2.
Day 2 · Warm-up: What does a skipped variable hold?
Read the Think First box at the top of worksheet Part A: a new object’s constructor skips one of its int instance variables. Does that variable hold nothing, a random leftover number, or something definite? Take a hand vote for each. Do not answer; slide 15 settles it in five minutes, and students should remember their vote. Remind them that on Day 1 every bug on slide 12 “stayed 0.” Today explains why.
Day 2 · Direct instruction: Default values and the default constructor
- Every instance variable starts with a default (3.4.A.5, slides 14–15). Before any constructor code runs, Java gives each instance variable a default based on its type:
0forint,0.0fordouble,falseforboolean, andnullfor any reference type. Write the four on the board; students copy them. Ask students to predict the slide 15 output before you point to it: the constructor sets onlysubject, so it prints0 0.0andfalse null. Settle the warm-up vote: the value is definite. - Why still set every one? Run
PlayerForgot. It runs without an error and printsteam: nullandaverage: 0.0. That is the danger: anullcauses trouble much later, far from the constructor. A constructor should set every instance variable on purpose, even when the value it wants matches the default. - The default constructor (3.4.A.4, slide 16). Left side:
Timerhas no constructor written, so Java supplies one with no parameters and an empty body, andnew Timer()works;secondskeeps its default, soshow()prints0. Right side: writing one constructor means Java supplies nothing else, sonew Timer()fails. RunDefaultLamp, thenNoDefaultAnymore. Close the loop from Day 1:BookVoidsaid “required: no arguments” because with the constructor turned into a method,Bookhad no constructor written, so Java supplied the default one.
Misconception: An unassigned instance variable holds garbage or nothing
Students who have heard that uninitialized variables are “undefined” expect random values or an error. Instance variables always start at their type’s default, so the output is predictable. That is why a skipped variable is a logic error, not a crash. (Local variables are different: code that uses a local variable before giving it a value does not compile.)
Misconception: Expecting the default constructor after writing your own
Students think every class always has a no-parameter constructor. Java supplies it only when the class has no constructor written. As soon as there is one, new ClassName() compiles only if the class also has a no-parameter constructor written by hand. Slide 16’s two columns are the contrast to point back to.
Day 2 · Guided practice: Two constructors, one forgets
- Slide 17. Students write the output and say which constructor runs each time.
new Plant(8)matchesPlant(int):heightis 8,wetistrue.new Plant()matchesPlant(), which sets onlyheightto 5, sowetkeeps its defaultfalse. Output:8 truethen5 false. Two constructors with different parameter lists are overloaded (Topic 1.13). Ask what one line would fix the second constructor (wet = false;ortrue, chosen on purpose). - Worksheet question 1 in pairs. Students fill in all five values for
new Kite("red", 5). Watch forwindSpeed:feet / 2isintdivision, so it is2, stored in adoubleas2.0(review from Topic 1.3). The last row, “Wouldnew Kite()compile?”, is no:Kitehas a constructor written.
Teacher note
Question 1 key: "red", 0, 2.0, false, null, and no. Students who write 2.5 for windSpeed have forgotten
integer division; students who write “nothing” for owner have not yet connected null to reference types. Settle
both before moving on.
Day 2 · Direct instruction: Mutable objects as parameters
- Copying a reference is not copying the object (3.4.A.3, slides 18–19). An object whose state can change after it is built is mutable. Students already know one: a
Scannermoves forward each time it reads (Topic 2.6). In theQuizconstructor,source = wordList;copies the reference, so the quiz andmainshare oneScanner. The quiz readscat, somainreadsdog. RunGraderSharedfor the same effect: the grader readsB, andmaingetsD. - Store a copy (slide 20). When a constructor receives a mutable object, the instance variable should hold a copy: a new object with the same values, built with
new. Then the class cannot change the caller’s object, and the caller’s changes cannot reach inside. Compare the two constructors: the copy printsfalseforshipTo == destination; the shared one printstrue. RunOrderCopy. - What about
String? Ask: “Bookstored itsStringparameter directly. Is that a problem?” No: aStringis not mutable (Topic 1.15), so sharing one cannot cause a surprise. The copy rule is only for objects that can change.
Teacher note
Address has a second constructor, Address(Address original), that builds a new Address with the same values; slide 20 states it. In OrderCopy, Address also has a moveTo method so that it is mutable. Tell students they only need to know that calling moveTo changes the street; writing methods is Topic 3.5. A Scanner cannot be copied this way, which is why the copy example switches to Address.
Misconception: Thinking call by value already protects objects
Students remember from Topic 1.13 that parameters get copies, and conclude that the object was copied. Call by value copies the argument, and for an object the argument is a reference. Draw two sticky notes with the same address pointing at one house: copying the note does not build a second house. Only new builds a new object, so a real copy always involves new.
Day 2 · Guided practice: Copy or share?
Worksheet question 9, alone, then compare with a partner. D, music = new Playlist(startMusic);, is correct. A shares the caller’s Playlist; B runs backward and leaves music as null; C builds a copy but stores it in a new local variable that vanishes, so music stays null. Point out that B and C are Day 1’s mistakes coming back.
Day 2 · Independent practice: Independent practice
Worksheet questions 3, 4, 5, 8, and 10 are homework (about 20 minutes). Question 3 is code tiles with two overloaded Frog constructors (the decoys new Frog(2, "Pip") and new Frog() do not compile); question 4 is a snippet pool where the order of the constructor’s lines matters (hours * 12.5 gives 0.0 because hours still holds its default); question 5 is you’re-the-compiler with a class that has a constructor and one that does not; question 8 is two truths and a lie about a shared Scanner (the lie is claim 3); question 10 is who-am-I vocabulary.
Day 2 · Closure: Closure
Slide 21 lists the vocabulary; read it quickly. On slide 22, partners finish the sentence starter: “A constructor gives every instance variable a starting value. If it skips one, that variable holds its default value (0, 0.0, false, or null).” Call on two pairs, one to say what a constructor must do and one to name a way it can go wrong.
Day 2 · Exit ticket: Exit ticket
Give the exit ticket for the last 5 minutes. Score each question separately: question 1 checks default values, the default constructor, and what the constructor sets (3.4.A.2, 3.4.A.4, 3.4.A.5); question 2 checks the copy rule for a mutable parameter (3.4.A.3); question 3 checks recognizing a constructor that sets every instance variable (3.4.A.1, 3.4.A.2). Re-teach the weakest one at the start of Topic 3.5.