Skip to content

Course navigation

For teachersTopic 3.4 · Lesson kit

Constructors

  • 2 class periods
  • 45 minutes each
  • Objective 3.4.A
  • 6 downloads with keys

Downloads

  • Slides

    Speaker notes on every slide

  • Lesson plan

    2 periods of 45 minutes

  • Worksheet

    Puzzles, about 30 minutes

  • Worksheet key

    Answer key

  • Exit ticket

    End of the last period

  • Exit ticket key

    Answer key

Download all AP CSA teacher filesEvery topic's slides, lesson plan, worksheet, and exit ticket, with keys (.zip)

At a glance

Pacing
2 class periods of 45 minutes
Learning objectives
3.4.A
Essential knowledge
3.4.A.13.4.A.23.4.A.33.4.A.43.4.A.5
Materials
Projector and the Starting State slide deckWhiteboard and markersMini whiteboards or scrap paperIDE on the teacher computer (demonstrations both days)Printed Starting State puzzle worksheet (answer key for you) and exit ticket

45 minutes a day

Agenda

Day 145 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  1. 0:00–0:055 minWarm-upa brand-new bike (slide 2)
  2. 0:05–0:2015 minDirect instructionstate, has-a, and the parts of a constructor (slides 3–8)
  3. 0:20–0:277 minGuided practicetwo tickets, one constructor (slide 9)
  4. 0:27–0:3710 minDirect instructionparameters supply the values, three ways to miss (slides 10–12)
  5. 0:37–0:425 minGuided practicewhich one stores the volume? (slide 13)
  6. 0:42–0:453 minClosurestore it or lose it

Day 245 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  • Exit ticket
  1. 0:00–0:055 minWarm-upwhat does a skipped variable hold? (worksheet Part A Think First)
  2. 0:05–0:138 minDirect instructiondefault values and the default constructor (slides 14–16)
  3. 0:13–0:2310 minGuided practicetwo constructors, one forgets (slide 17) and worksheet question 1
  4. 0:23–0:329 minDirect instructionmutable objects as parameters (slides 18–20)
  5. 0:32–0:375 minGuided practiceworksheet question 9 (copy or share?)
  6. 0:37–0:403 minClosureborn ready (slides 21–22)
  7. 0:40–0:455 minExit ticket

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

  1. Goals (slide 3). Read the six goals. Goals 1–4 are today’s; 5 and 6 are Day 2’s.
  2. 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.0 and false; bike 52 has 38.5 and true. 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.
  3. 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 even void, the parameter list, and a body that gives every instance variable a value. Point at battery = 100.0; and rented = false;: a starting value can be fixed by the constructor instead of coming from a parameter. That answers the warm-up.
  4. 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, with bikeId holding a copy of 417; a reference is handed back and stored in rental. Ask why 417 100.0 prints between Start and Done: the constructor finishes before main moves on. Run BookCreation in the IDE to show the same order with a Book.
  5. Two objects, two references. Run ScoreboardStart: two scoreboards with identical states, and == prints false. Each new sets aside separate memory and returns a different reference (Topic 2.6). Same state does not mean same object.
  6. A return type turns it into a method (slide 8). Show BookVoid in the IDE and ask students to predict before compiling. With void, 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

  1. 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 Backpack constructor, name is copied from owner, pockets is fixed at 4, and heavy is computed from pounds. Every instance variable gets a value, by one of those three routes. Run PetCreation: same constructor, different arguments, different starting states.
  2. 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: “Backpack has no instance variable for pounds. Where is 12.5 after the constructor ends?” Gone, and that is fine here, because only heavy needed it.
  3. Three ways to never set an instance variable (slide 12). Go row by row and ask what battery holds after new 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 =.
  4. Same name, live. Run PetSameName. It compiles and prints A Pet was created., but the pet never gets its name or age. Inside the constructor the name name means the parameter, so name = name; copies the parameter into itself. Rename the parameters petName and petAge, assign name = 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

  1. 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: 0 for int, 0.0 for double, false for boolean, and null for 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 only subject, so it prints 0 0.0 and false null. Settle the warm-up vote: the value is definite.
  2. Why still set every one? Run PlayerForgot. It runs without an error and prints team: null and average: 0.0. That is the danger: a null causes 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.
  3. The default constructor (3.4.A.4, slide 16). Left side: Timer has no constructor written, so Java supplies one with no parameters and an empty body, and new Timer() works; seconds keeps its default, so show() prints 0. Right side: writing one constructor means Java supplies nothing else, so new Timer() fails. Run DefaultLamp, then NoDefaultAnymore. Close the loop from Day 1: BookVoid said “required: no arguments” because with the constructor turned into a method, Book had 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

  1. Slide 17. Students write the output and say which constructor runs each time. new Plant(8) matches Plant(int): height is 8, wet is true. new Plant() matches Plant(), which sets only height to 5, so wet keeps its default false. Output: 8 true then 5 false. Two constructors with different parameter lists are overloaded (Topic 1.13). Ask what one line would fix the second constructor (wet = false; or true, chosen on purpose).
  2. Worksheet question 1 in pairs. Students fill in all five values for new Kite("red", 5). Watch for windSpeed: feet / 2 is int division, so it is 2, stored in a double as 2.0 (review from Topic 1.3). The last row, “Would new Kite() compile?”, is no: Kite has 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

  1. 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 Scanner moves forward each time it reads (Topic 2.6). In the Quiz constructor, source = wordList; copies the reference, so the quiz and main share one Scanner. The quiz reads cat, so main reads dog. Run GraderShared for the same effect: the grader reads B, and main gets D.
  2. 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 prints false for shipTo == destination; the shared one prints true. Run OrderCopy.
  3. What about String? Ask: “Book stored its String parameter directly. Is that a problem?” No: a String is 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.

Exit ticket

Question 1 · Multiple choice

ExitLantern.java
public class ExitLantern
{
    public static void main(String[] args)
    {
        Lantern camp = new Lantern("green", 20);
    }
}

class Lantern
{
    private String color;
    private int hours;
    private double fuel;
    private boolean lit;

    public Lantern(String shade, int burnHours)
    {
        color = shade;
        fuel = burnHours / 8;
        System.out.println(color + " " + hours + " " + fuel + " " + lit);
    }
}

What does this program print, and would new Lantern() compile?

  1. It prints green 0 2.5 false; new Lantern() does not compile.
  2. It prints green 0 2.0 false; new Lantern() does not compile.
  3. It prints green 0 2.0 false; new Lantern() compiles and uses the default constructor.
  4. It prints green 0 2 false; new Lantern() does not compile.
Check your reasoning

B.

Instance variable Default After the constructor body
color null "green"
hours 0 still 0: never assigned
fuel 0.0 20 / 8 is 2 (int division), 2.0
lit false still false: never assigned

new Lantern() does not compile: Lantern has a constructor written, so Java does not supply the no-parameter default constructor, and no constructor matches a call with no arguments.

  • A does 20 / 8 as 2.5, but both are ints, so the division drops the remainder before the value is stored in fuel.
  • C has the right output, but Java supplies a default constructor only when a class has no constructor written.
  • D prints fuel as an int; storing 2 in a double makes it 2.0.

Question 2 · Multiple choice

Itinerary is a mutable class with a constructor Itinerary(Itinerary original) that builds a copy. A class Trip has private Itinerary plan;. Which line belongs in the constructor public Trip(Itinerary startPlan)?

  1. plan = startPlan;
  2. Itinerary plan = new Itinerary(startPlan);
  3. plan = new Itinerary(startPlan);
  4. startPlan = new Itinerary(plan);
Check your reasoning

C. When a constructor receives a mutable object, the instance variable should hold a copy, so the trip and the caller never share one Itinerary. Only new builds that copy.

  • A is wrong: it stores the caller’s reference, so a change made through either variable shows up in both.
  • B is wrong: it builds a copy but stores it in a new local variable that vanishes when the constructor ends, so plan stays null.
  • D is wrong: it runs backward, assigning to the parameter; plan is never set and stays null.

Question 3 · Multiple choice

A class Aquarium has the instance variables private String name;, private int fish;, and private boolean heater;. Which constructor takes the tank’s name and number of fish and starts every new tank with the heater off?

  1. public Aquarium(String tankName, int fishCount) { name = tankName; fish = fishCount; heater = false; }
  2. public void Aquarium(String tankName, int fishCount) { name = tankName; fish = fishCount; heater = false; }
  3. public Aquarium(String name, int fish) { name = name; fish = fish; heater = false; }
  4. public Aquarium(String tankName, int fishCount) { String name = tankName; int fish = fishCount; heater = false; }
Check your reasoning

A. The header has no return type, and each instance variable is on the left of =, set from a parameter. heater = false; is written on purpose, even though false is the default. In this program it prints Reef 12 false (the println line is only there to show the state it set):

ExitAquarium.java
public class ExitAquarium
{
    public static void main(String[] args)
    {
        Aquarium lobby = new Aquarium("Reef", 12);
    }
}

class Aquarium
{
    private String name;
    private int fish;
    private boolean heater;

    public Aquarium(String tankName, int fishCount)
    {
        name = tankName;
        fish = fishCount;
        heater = false;
        System.out.println(name + " " + fish + " " + heater);
    }
}
  • B has void in the header, which makes it a method, not a constructor. Java then supplies the default constructor, so new Aquarium("Reef", 12) does not compile.
  • C names the parameters name and fish, so name = name; copies each parameter into itself. The instance variables stay null and 0.
  • D declares new local variables name and fish, which vanish when the constructor ends. The instance variables stay null and 0.