These notes recap Topic 3.4. Full explanations and worked examples are in the study guide.
Recap
- An object’s state is its instance variables and their values at a given time (a has-a relationship).
- A constructor sets the initial state, including values for all instance variables. It is
public, named like the class, and has no return type. - When a constructor is called, memory is allocated and the reference to the object is returned.
- Constructor parameters supply data to initialise the instance variables.
- If no constructor is written, Java provides a no-parameter default constructor; instance variables get default values.
- When a mutable object is a constructor parameter, initialise the instance variable with a copy so the class’s state cannot be changed through the original reference.
Key relationships
| Situation | What happens |
|---|---|
new Player("Asha") with a one-parameter constructor |
name = “Asha”, lives = 3 |
new Player() when only parameterised constructors exist |
Does not compile |
| No constructor written | Default no-parameter constructor; fields at defaults |
seat = s; |
Same reference: two names, one object |
seat = new Seat(s.getRow(), s.getNumber()); |
A copy: two objects |
Default values
| Type | Default |
|---|---|
int |
0 |
double |
0.0 |
boolean |
false |
reference (e.g. String) |
null |
Worked reminder
new Player("Ben", 5) allocates a Player, sets name to null and lives to 0 for an instant, runs the two-parameter constructor (so name becomes “Ben” and lives becomes 5) and returns the reference, which is stored in the variable. With Seat original = new Seat(4, 12), a booking that does seat = s; follows original.setNumber(99) and reports 99, whereas a booking that builds new Seat(s.getRow(), s.getNumber()) still reports 12. Both facts come from the same rule: a variable of a class type holds a reference.
Assumptions and checks
- Overloaded constructors share the class name and differ in the number or types of parameters.
- Parameter names that differ from instance variable names avoid confusion (Topic 3.8 covers the same-name case).
- Check every line that creates an object against the constructors actually written in the class.
Mistakes to avoid
- Writing a return type before the constructor name, which turns it into a method.
- Forgetting that adding a constructor removes the default one.
- Leaving an instance variable unassigned and relying on a default that does not suit the specification.
- Assuming
seat = s;copies the object. - Mixing up the order of arguments when two parameters have the same type.
Quick self-check
- What is printed by a default-constructed object with
int count; String label;and getters printed in that order? Answer:0 null. - A class has only
public Pet(String n). Doesnew Pet()compile? Answer: No. - After
Seat s1 = new Seat(1, 5); Seat s2 = s1; s2.setNumber(9);what doess1.getNumber()return? Answer: 9, because both names refer to the same object. - Why store a copy of a mutable parameter? Answer: so code outside the class cannot change the object’s state by altering the original.