Topic 1.13 Notes – Object Creation and Storage (Instantiation)
What Constructors Are and How They Are Identified
A constructor is a special method that runs when you create a new object.
You can spot one because:
- It has the same name as the class.
- It has no return type (not even
void). - It runs automatically when you use
new.
Example:
public class Player {
private String name;
private int score;
public Player(String n, int s) {
name = n;
score = s;
}
}
That Player(String n, int s) is the constructor.
Constructor Signature
The signature determines which constructor gets called.
Signature = constructor name + ordered list of parameter types
Important details:
- Parameter names do not matter.
- Order matters.
- Types matter.
So these are different:
Player(String name, int score)
Player(int score, String name)
Same types, different order → different signatures.
On multiple choice, they love giving you several constructors and asking which line compiles. Match:
- Number of arguments
- Types
- Order
Miss one of those and it won’t match.
Overloaded Constructors
A class can have multiple constructors as long as their signatures are different. That’s called constructor overloading.
Example:
public Player(String n) {
name = n;
score = 0;
}
public Player(String n, int s) {
name = n;
score = s;
}
Java chooses based on the arguments you pass:
Player p1 = new Player("Alex"); // first constructor
Player p2 = new Player("Jamie", 10); // second constructor
If no constructor matches exactly, you get a compile-time error.
Object References and Variable Declarations
When you declare an object variable:
Player p;
You are creating a reference variable.
That variable:
- Can store a reference to a
Playerobject - Or can store
null
The object itself lives somewhere in memory. The variable just holds the reference (address) to it.
Here’s the mental model. Focus on how the variables in the stack point to objects in the heap:

Stack and heap memory with object references
Key ideas:
- Multiple variables can refer to the same object.
- Assigning one reference to another does not create a new object.
- If a reference hasn’t been assigned an object, it is
null.
Example:
Player p1 = new Player("Alex", 5);
Player p2 = p1;
Now p1 and p2 point to the same object. Changing through one affects the same object.
This shows up on tests when they ask whether two variables refer to the same object or different ones. Track arrows, not names.
Creating Objects with new
Standard pattern:
ClassName variable = new ClassName(arguments);
Example:
Player p = new Player("Taylor", 7);
What Happens When new Runs
When Java executes that line:
- Memory is allocated for the object.
- The constructor runs.
- The constructor initializes instance variables.
- The reference is returned and stored in
p. - Execution continues after the line.
The constructor temporarily interrupts normal flow, runs its body, then control returns right after the call.
If you forget new:
Player p = Player("Taylor", 7); // compile error
Java thinks you’re calling a method, not creating an object.
Parameter Matching and Call by Value
Arguments must match:
- Same number
- Same types
- Same order
Java uses call by value:
- The constructor parameters receive copies of the argument values.
- Changing the parameter inside the constructor does not change the original variable.
That detail sometimes appears in tricky tracing questions.
How Constructors Initialize Object State
Constructors usually assign parameters to instance variables.
public Player(String n, int s) {
name = n;
score = s;
}
Each object gets:
- Its own copy of instance variables
- Access to the same shared methods
If you create two objects:
Player a = new Player("A", 3);
Player b = new Player("B", 8);
They have separate name and score values. Changing a does not affect b.
This independence is critical in FRQ2. If your constructor fails to initialize variables properly, the rest of your class logic breaks.
How This Shows Up on Tests
You’ll commonly see:
- “Which constructor is called?”
- “Which line compiles?”
- “What is the state of the object after this code runs?”
- “Do these variables refer to the same object?”
On class-design FRQs:
- You must initialize all required instance variables.
- If the prompt gives sample objects, use them to reverse-engineer what the constructor should do.
Most constructor errors on quizzes come from mixing up parameter order or forgetting that the signature depends on types, not parameter names.