The Person blueprint
A class is a blueprint: Person declares the attributes name and age and the method greet(). The blueprint defines the structure, but by itself it occupies no object memory in the Heap yet.
A class is a blueprint: Person declares the attributes name and age and the method greet().
The blueprint defines the structure, but by itself it occupies no object memory in the Heap yet.
A class is a blueprint: Person declares the attributes name and age and the method greet(). The blueprint defines the structure, but by itself it occupies no object memory in the Heap yet.
The light blue particle is the execution flow. In main, the new operator requests memory in the Heap.
An instance of Person is born with default values: name set to null and age set to 0.
The light blue particle is the execution flow. In main, the new operator requests memory in the Heap. An instance of Person is born with default values: name set to null and age set to 0.
new returns the address of the Heap object and stores it in the p1 variable on the Stack.
p1 does not hold the object inside: it stores a reference pointing to the instance.
new returns the address of the Heap object and stores it in the p1 variable on the Stack. p1 does not hold the object inside: it stores a reference pointing to the instance.
With the dot operator (.), p1.name and p1.age follow the reference into the Heap.
The default values change to "Laura" and 28 inside that specific object.
With the dot operator (.), p1.name and p1.age follow the reference into the Heap. The default values change to "Laura" and 28 inside that specific object.
Another call to new Person() allocates a second independent object in the Heap.
Each instance holds its own state: p2 receives "Carlos" without altering p1.
Another call to new Person() allocates a second independent object in the Heap. Each instance holds its own state: p2 receives "Carlos" without altering p1.
When invoking p1.greet(), the method reads the attributes of that object and prints "Laura".
Then p2.greet() runs the same blueprint code, but reading the state of p2.
When invoking p1.greet(), the method reads the attributes of that object and prints "Laura". Then p2.greet() runs the same blueprint code, but reading the state of p2.
Watch out for Person p2 = p1: without new, the assignment only copies the reference on the Stack.
Both variables point to the same object in the Heap: no clone was created.
Modifying p2.name mutates that single shared object, and p1.name is now "Ana Maria".
Watch out for Person p2 = p1: without new, the assignment only copies the reference on the Stack. Both variables point to the same object in the Heap: no clone was created. Modifying p2.name mutates that single shared object, and p1.name is now "Ana Maria".
The class is the blueprint; each object created with new is an independent instance in the Heap.
Variables on the Stack store references and the dot operator accesses the targeted object.
Only new allocates another object; assigning p2 = p1 shares the same instance.
The class is the blueprint; each object created with new is an independent instance in the Heap. Variables on the Stack store references and the dot operator accesses the targeted object. Only new allocates another object; assigning p2 = p1 shares the same instance.