KTU OOP Java2024 Scheme • 60M ESE
MODULE 1/Section 3: Object-Oriented Programming in Java

Declaring Objects, Object References, new Operator & Method Invocation

HIGH PRIORITY (Prep Guide)Official Syllabus~14 min study
Official KTU 2024 Syllabus Topics Covered Here
Declaring objectsObject referencesObject creation using newIntroduction to methodsObject initialization and 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.

2. Start From Zero: House Key (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!

Key Takeaway: 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

Object
A runtime instance of a class possessing three characteristics: State (field values), Behavior (methods), and Identity (unique runtime existence/memory reference).
Object Reference Variable
A variable of a class type (e.g., Student s1) that holds the memory reference (address handle) of an object on the Heap, or null if pointing to nothing.
The new Operator
Operator that dynamically allocates Heap memory for an object at runtime, initializes default field values, executes the class constructor, and returns the object's reference.

4. 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. 1. Stack slot s is reserved in the current method's stack frame.
  2. 2. new checks Heap space and allocates an object block large enough for all instance variables (initializing defaults 0, false, null).
  3. 3. Constructor Student() executes to initialize state.
  4. 4. new returns the Heap reference, which = stores inside s.
  5. 5. s.display() passes s implicitly (as this) into display() so it reads that specific object's fields.

6. Examination-Ready Diagrams

Diagram 4

Class and Object Relationship (Stack & Heap)

Class: Student (Blueprint)Stored once in Method Areaint rollNo;String name;void display() {...}STACK (References)s1Ref: 0x104As2Ref: 0x208CHEAP (Objects via new)Student Object @ 0x104ArollNo = 101, name = "Asha"Student Object @ 0x208CrollNo = 102, name = "Kiran"

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.

Key Components
  • Class Blueprint: Defines fields (rollNo, name) and methods (display()) once in the Method Area.
  • Stack Frame (References): Holds reference variables s1 and s2 storing memory addresses.
  • Heap Memory (Objects): Holds independent physical object instances created via new Student().
Step-by-Step Flow
  1. Student s1; declares a reference variable s1 on Stack (initially null or unassigned).
  2. new Student() allocates memory on Heap with default field values and invokes constructor.
  3. = stores the Heap address inside s1; creating s2 allocates a separate Heap object.
How to Draw in KTU Exam
  • 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

Object Creation, Reference Aliasing & Method InvocationBoxReferenceDemo.java
Demonstrates independent Heap objects vs reference aliasing (b2 = b1) and invoking methods using the dot operator.
1class 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
17public 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}
Expected Output
Volume of b1: 3000.0
Volume of b2: 162.0
b1 volume after modifying b3.width: 6000.0
Keywords:classnewvoidreturn
Common Exam & Coding Mistakes
  • Writing Box b1; b1.width = 10; without calling new Box() (compile error for local variable, or NullPointerException if initialized to null).
  • 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 new and 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 null reference (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 (null initially 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 to b1.
  • 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.
Exam Diagram Note: Draw Diagram 4 showing Stack (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?