Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
A class is a blueprint. An object is the real thing built from that blueprint. Blueprint of a house → Class. Actual house built from it → Object.
What a Class Contains
javascript
QUBITS OF DPK
Code Example
java
QUBITS OF DPK
Memory Model
javascript
QUBITS OF DPK
Reference Sharing Trap
java
QUBITS OF DPK
this Keyword
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"A class is a blueprint defining fields (properties) and methods (behaviors). An object is a real instance created with 'new' keyword — reference stored in stack, actual object in heap. Multiple objects from one class each get independent copies of instance fields. 'this' keyword refers to the current object — used to disambiguate between field and parameter with same name. Key trap: two references pointing to same object means modification via one reflects in the other — called reference sharing, fundamental to linked lists and trees in DSA."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between class and object?
A class is a blueprint/template defining fields and methods — it has no memory allocated. An object is a real instance created from the class using new — memory is allocated in heap. Multiple objects can be created from one class, each with independent field values. Analogy: Class = house blueprint (one). Objects = actual houses built from it (many).
Q2. Where is the object stored — stack or heap?
The object is stored in Heap memory. The reference variable is stored in Stack. Example: Car myCar = new Car() — myCar (the reference/address) lives in stack, the actual Car object lives in heap. This is why when you pass an object to a method, you pass the reference (stack value), and both the caller and callee can modify the same heap object.
Q3. What is the this keyword used for?
this refers to the current object instance. Five uses: (1) disambiguate field vs parameter with same name: this.speed = speed, (2) call another constructor: this("BMW", 100) (must be first line), (3) pass current object to a method: engine.attach(this), (4) return current object for method chaining (Builder pattern): return this, (5) access outer class from inner class. Cannot use this in static methods since static has no object context.
Q4. What happens when two references point to the same object?
Both see the same data and any modification through one is visible through the other:
java
QUBITS OF DPK
This is called reference sharing. Critical in DSA: LinkedList nodes, Tree nodes, and Graph nodes all use this. Setting a = null does NOT destroy the object if b still points to it.
Q5. What is NullPointerException and when does it occur?
NPE is thrown when you try to use a null reference — call a method, access a field, or use it as an array. String s = null; s.length() throws NPE. Common causes: uninitialized object reference, method returning null not checked, array of objects before initializing elements. Prevention: null checks before use, Optional<T> in Java 8+, @NonNull annotations in frameworks like Spring.