Declaring Objects, Object References, new Operator & Method Invocation
1. Topic Introduction
Defining a class creates only a logical blueprint—no memory is reserved for student records or bank accounts until you instantiate objects. Understanding the crucial two-step distinction between declaring an object reference variable and allocating an actual Heap object with new unlocks how Java handles references, aliasing, and method calls.
Reference) vs Actual Built House (Heap Object)Writing Student s1; does NOT build a house! It simply creates an empty key tag (s1) on your keychain (Stack). Only when you write new Student() does Java construct an actual house in the neighborhood (Heap) and stamp its address onto key s1. Even better: if you write Student s2 = s1;, you haven't built a second house—you merely copied the key! Changing the living room paint via s2.name = "Aria" changes the exact same house that s1 unlocks!
ClassName ref; creates a reference variable on Stack; new ClassName() dynamically allocates the real object on Heap and returns its reference address.3. Essential Exam Definitions
Student s1) that holds the memory reference (address handle) of an object on the Heap, or null if pointing to nothing.new Operator4. Detailed Technical Explanation & Syntax
Two-Step Object Creation & Reference Aliasing
Creating an object conceptually happens in two steps (usually combined on one line):
1. Declaration (Box b1;): Declares a reference variable b1 on the Stack. No Box object exists yet; dereferencing b1 throws NullPointerException (or compile error if local & uninitialized).
2. Dynamic Allocation (b1 = new Box();): new allocates memory on the Heap for width, height, depth, runs the constructor, and assigns the Heap reference to b1.
Reference Assignment (Box b2 = b1;): Copies the reference, NOT the object! Both b1 and b2 now refer to the exact same Heap object. Assigning b1 = null; disconnects b1, while b2 keeps the object alive.
Object State, Behavior, Identity & Dot (.) Method Invocation
Every Java object exhibits State (instance fields like rollNo, name), Behavior (member methods like calculateVolume()), and Identity (distinct Heap allocation compared via ==).
Using the dot operator (b1.computeVolume()), you invoke a method on the specific object b1, so the method operates directly on b1's own instance variables.
5. Working Principle: What Student s = new Student(); Actually Executes
- 1. Stack slot
sis reserved in the current method's stack frame. - 2.
newchecks Heap space and allocates an object block large enough for all instance variables (initializing defaults0,false,null). - 3. Constructor
Student()executes to initialize state. - 4.
newreturns the Heap reference, which=stores insides. - 5.
s.display()passessimplicitly (asthis) intodisplay()so it reads that specific object's fields.
6. Examination-Ready Diagrams
Class and Object Relationship (Stack & Heap)
Illustrates how a Class acts as a blueprint, how reference variables live on the Stack, and how new allocates distinct Object instances on the Heap.
- Class Blueprint: Defines fields (
rollNo,name) and methods (display()) once in the Method Area. - Stack Frame (References): Holds reference variables
s1ands2storing memory addresses. - Heap Memory (Objects): Holds independent physical object instances created via
new Student().
Student s1;declares a reference variables1on Stack (initiallynullor unassigned).new Student()allocates memory on Heap with default field values and invokes constructor.=stores the Heap address insides1; creatings2allocates a separate Heap object.
- Draw a 'Class: Student' box on the left (Blueprint), a 'Stack' column in the middle (
s1 -> 0x100,s2 -> 0x200), and a 'Heap' area on the right with two separate object boxes. - Use arrows from stack references to their respective heap objects to show independent state.
7. Complete Java Code Examples & Tracing
b2 = b1) and invoking methods using the dot operator.| 1 | class Box { |
| 2 | double width; |
| 3 | double height; |
| 4 | double depth; |
| 5 | |
| 6 | void setDimensions(double w, double h, double d) { |
| 7 | width = w; |
| 8 | height = h; |
| 9 | depth = d; |
| 10 | } |
| 11 | |
| 12 | double computeVolume() { |
| 13 | return width * height * depth; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | public class BoxReferenceDemo { |
| 18 | public static void main(String[] args) { |
| 19 | // Step 1 & 2 combined: separate objects on Heap |
| 20 | Box b1 = new Box(); |
| 21 | Box b2 = new Box(); |
| 22 | |
| 23 | b1.setDimensions(10, 20, 15); |
| 24 | b2.setDimensions(3, 6, 9); |
| 25 | |
| 26 | System.out.println("Volume of b1: " + b1.computeVolume()); |
| 27 | System.out.println("Volume of b2: " + b2.computeVolume()); |
| 28 | |
| 29 | // Reference Aliasing: b3 points to the SAME object as b1! |
| 30 | Box b3 = b1; |
| 31 | b3.width = 20; // Doubles b1's width too! |
| 32 | System.out.println("b1 volume after modifying b3.width: " + b1.computeVolume()); |
| 33 | } |
| 34 | } |
Volume of b1: 3000.0 Volume of b2: 162.0 b1 volume after modifying b3.width: 6000.0
classnewvoidreturn- Writing
Box b1; b1.width = 10;without callingnew Box()(compile error for local variable, orNullPointerExceptionif initialized tonull). - Assuming
Box b3 = b1;creates a deep copy of the object instead of sharing the same object reference.
Real-World Applications
- Multiple UI components or listeners holding references to the same shared Data Model object.
Important KTU Exam Points
- Clearly draw Stack (reference variables) vs Heap (actual object instances) when explaining
newand object references. - Explain the three traits of an object: State, Behavior, and Identity.
Memory Techniques & Revision
- Object Traits = 'S-B-I': State (fields), Behavior (methods), Identity (memory handle).
Common Pitfalls to Avoid
- Calling methods on a
nullreference (NullPointerException).
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: In Java, creating an object is a two-step mechanism involving declaring a class-type reference variable and dynamically allocating memory on the Heap.
- 1. Declaring an Object Reference (
Box b1;): Creates a reference variable on the Stack capable of holding a memory reference (nullinitially if field; unassigned if local). It does NOT allocate an object. - 2. Creating Object using
new(b1 = new Box();): Dynamically allocates Heap memory for instance fields at runtime, invokes the constructor, and assigns the resulting reference tob1. - Reference Aliasing: Assigning
Box b2 = b1;makes both variables point to the same Heap object. - Method Invocation:
b1.computeVolume()accesses the object's methods via the dot (.) operator.
b1, b2) pointing to Heap Box objects.Quick Understanding Check
1. After Student s1 = new Student(); Student s2 = s1;, how many Student objects exist on the Heap?