Object Instantiation and Memory Allocation: Trace Object Creation Step by Step
Follow two Java traces to count objects and references, separate aliases from copies, and reason correctly about identity, mutation, and reachability.
KnowledgeGate Team
Exam prep & CS education

You see new, a constructor call, three variables and two assignments. How many objects exist? When creation and assignment blur together, object-counting becomes guesswork. The same confusion makes field-value and output questions unreliable after an alias mutates. The Java example traces object creation, and the reachability example traces when objects become eligible for garbage collection. The first finishes with p and q reaching O1 (x=9, y=4), while r reaches O2 (x=9, y=7): 2 objects and 3 non-null references.
1. Instantiation, allocation, initialization and references are different ideas
A class is a declared type; an object is a runtime instance with identity and state. Instantiation creates it, allocation reserves storage, initialization sets starting state, and a reference lets code reach it.
Point p; declares a Java local reference and creates 0 Point objects. Point p = null; also creates 0. Add an object only when an expression creates one, as new Point(3, 4) does.
The boxes and arrows model variables, references, objects and fields, not physical addresses, byte counts, or a universal stack and heap layout. For blueprint, static-member and per-object-state rules, use Classes and Objects in OOP: Blueprint, Instance and Worked Example. The Point and Box traces below instead isolate allocation counts, aliases, constructor invocations and garbage-collection reachability.
2. What one new Point(3, 4) expression does
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Point p = new Point(3, 4);Java evaluates 3 and 4, creates O1, gives its fields default values x=0 and y=0, then runs the constructor assignments. The resulting reference enters p. End state: p -> O1(x=3, y=4), 1 object, 1 constructor invocation.
The code does not reveal O1's total byte size. Headers, alignment, compression and runtime strategy are unspecified. For more syntax and coding practice, use the Java Course.
3. Worked trace: two allocations, one alias and one independent object
Point p = new Point(3, 4); // creates O1
Point q = p; // q aliases O1
q.x = 9; // mutates O1
Point r = new Point(p.x, p.y); // creates O2 from values 9 and 4
r.y = 7; // mutates O2 onlyTrace it line by line:
p -> O1(3,4). Count: 1 object.qreceives the same reference. Both names reach O1; the count stays 1.q.x = 9changes O1 to(9,4), visible through both names.The current values
9and4enter a new constructor call. Distinct O2 becomes(9,4), reached byr.r.y = 7changes only O2 to(9,7).
Final answer: O1 is (9,4) through p and q; O2 is (9,7) through r; 2 objects were created; 3 references are non-null; and the constructor ran 2 times. p == q is true; the other two identity comparisons are false. q = p neither clones nor constructs an object.

4. Assignment may copy a reference or copy state
In Java, Point q = p copies a reference, making the names aliases for O1. new Point(p.x, p.y) creates O2 from two primitive values. O2 initially matches O1's state, not its identity. Changing r.y proves their independence.
After the main trace, execute Point s = null; s = p;. It creates 0 objects and invokes 0 constructors. The total stays 2; s is a fourth non-null reference to O1; and s == q is true. After s.y = 12, O1 is (9,12) through p, q and s, while O2 stays (9,7).
For a C++ Point containing only int x and int y, Point a{3,4}; Point b = a; b.x = 9; creates two direct objects under ordinary value-copy semantics. Finally, a=(3,4) and b=(9,4). Check the language's copy rules before reusing a Java diagram.
5. Reachability is not immediate reclamation
Start a clean trace where Box(int value) stores its argument in one field:
Box a = new Box(10); // creates O1
Box b = new Box(20); // creates O2
b = a; // O2 loses its last reference
a = null; // O1 is still reachable through bAfter line 2, 2 objects exist and both references are non-null. After b = a, both variables point to O1. O2 is unreachable and eligible for garbage collection. After a = null, O1 remains reachable through b. Finally, a=null, b -> O1(value=10), 2 objects were created, 1 is reachable, and 1 is unreachable.
Eligibility does not mean immediate collection or a known time. Assigning null does not destroy O1 while b reaches it. Other lifetime models need other rules.

6. How assessments test object creation
Typical questions count executed new expressions and constructors, separate objects from references, trace aliasing, compare identities, and decide reachability. Practise the sourced question set and mixed-language lifetime drills in Instantiation MCQs: 6 Solved MCQs Plus 3 Object-Creation Problems. The Java traces here develop allocation, alias mutation, constructor counting and reachability from first principles.
Try Point a = new Point(2,5); Point b = a; a = new Point(8,1); b.y = 6;. The lines create O1 (2,5), alias it through b, create O2 (8,1) and redirect a, then change O1 through b. Final answer: a -> O2(8,1), b -> O1(2,6), 2 objects, 2 constructor calls, 2 non-null references, and a == b is false.
7. Common traps and their repairs
Tempting claim | What goes wrong | Repair |
|---|---|---|
Three variables mean three objects |
| Count executed creation operations and draw arrows |
| Mutation through either name reaches the same identity | Label O1 once and draw both arrows |
Same field values mean the same object | O1 and O2 both held | Test identity separately from state |
| O1 remains reachable through | Trace every live path before discussing eligibility |
Java instance fields receive default zero values before constructor assignments. An uninitialised local int z; cannot be read as zero. A constructor participates in initialization, but is not an ordinary method returning the object. The complete new Point(...) expression covers creation and construction.
A classroom picture may separate local variables from objects. Call it a conceptual reference-and-object diagram, not the exact JVM layout. It cannot prove addresses, byte sizes, or optimisations.
8. The short version and the next practice step
Keep five rules: a declaration can create 0 objects; each executed new Point(...) creates one; q = p creates an alias, not a clone; equal state does not prove equal identity; and collection eligibility begins only when no live path reaches an object. The final trace state is O1 (9,4), O2 (9,7), with 2 objects and 3 references.
For a five-minute drill, cover section 3, redraw five frames, and explain why the constructor count is 2. Then replace Point q = p with Point q = new Point(p.x, p.y) before q.x = 9. The result is 3 objects and 3 constructor calls: p -> O1(3,4), q -> O2(9,4), and r -> O3(3,7), because r reads unchanged p.
Continue with Coding for Placements for a broader multi-language placement route once the Java trace above feels routine.
Keep learning
Instantiation MCQs: 6 Solved MCQs Plus 3 Object-Creation Problems
Solve six published MCQs and three short object-creation problems. Each answer uses a concrete state, construction or lifetime trace.
Object-Oriented Programming Foundations: Core Concepts, a Worked Trace and Exam Traps
Connect the main OOP ideas through one Shape hierarchy. Trace two runtime objects, method dispatch, exact area calculations and the traps that turn familiar terms into wrong answers.
C++ Constructors and Object Initialization: Techniques, Order and a Worked Trace
Learn how C++ chooses default, parameterised, delegating and copy constructors, then trace member states and initialization order through complete code.
C++ Object Lifetime and Storage Duration: Trace Construction, Destruction, and Dangling State
Learn to separate scope, storage duration, lifetime, and ownership. Trace seven objects precisely, then repair dangling pointers, references, and views.





