0% found this document useful (0 votes)
6 views3 pages

Java Destructor GC Exam Notes With Code

A destructor is a method for destroying objects and freeing memory, but Java does not support destructors like C++. Instead, Java relies on the Garbage Collector (GC) to automatically remove unused objects when they become unreachable. The document also compares constructors and destructors, explaining their roles and providing a code snippet demonstrating how GC works in Java.

Uploaded by

raahimkhalid777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Destructor GC Exam Notes With Code

A destructor is a method for destroying objects and freeing memory, but Java does not support destructors like C++. Instead, Java relies on the Garbage Collector (GC) to automatically remove unused objects when they become unreachable. The document also compares constructors and destructors, explaining their roles and providing a code snippet demonstrating how GC works in Java.

Uploaded by

raahimkhalid777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Notes — Destructor (and Garbage

Collection)
Exam Style | Easy English | Short Paragraphs + Key Points | With Code Snippet

(Based on: DESTRUCTOR IN [Link])

1) What is a Destructor? (Basic Idea)


A destructor is a special method that is used to destroy an object and free memory when the
object is no longer needed.

 It works opposite to a constructor.


 Constructor → creates/initializes an object.
 Destructor → destroys an object (frees memory).

Exam Line: Destructor destroys an object and releases memory; it works opposite of a
constructor.

2) Important Point in Java (Most Asked)


Java does NOT have destructors like C++. Instead, Java uses the Garbage Collector (GC) to
automatically remove unused objects and free memory.

 No explicit destructor support in Java.


 Memory is freed automatically via Garbage Collection (GC).

One-Line Viva Answer


Java does not have destructors; memory is released automatically by the Garbage Collector.

3) How Object is Destroyed in Java? (GC Conditions)


In Java, an object becomes removable when it is no longer referenced (unreachable). Then
the Garbage Collector can remove it and free memory.

 Object is no longer referenced (no variable points to it).


 Reference is set to null.
 Program ends (objects become unreachable).
 Then Garbage Collector removes it.
4) Code Snippet (From PPT): finalize() + GC
The lecture shows a code example where finalize() prints a message when an object is
destroyed by the Garbage Collector.

class Test {

protected void finalize() {

[Link]("Object destroyed");

public static void main(String[] args) {

Test t1 = new Test();

t1 = null; // object eligible for GC

[Link](); // request garbage collector

// output: Object destroyed

Easy Explanation
 t1 = null makes the object unreachable (eligible for GC).
 [Link]() requests GC to run (GC timing is not guaranteed).
 finalize() prints the message when GC removes the object.

5) Constructor vs Destructor (Comparison)


Constructors initialize objects, while destructors remove objects and release memory. In
Java, removal is handled by GC.

 Purpose: Constructor initializes object | Destructor destroys object.


 Called: Constructor when object is created | Destructor when object is removed.
 In Java: Constructor supported | Destructor not explicitly supported.
 Automatic: Constructor yes | Destructor via Garbage Collector.
6) Real-Life Example (Easy Understanding)
 Constructor: When you buy a phone, setup starts (initialization).
 Destructor: When phone is discarded, it is removed and data is erased.
 In Java: the “Cleaner” (GC) does discarding automatically.

7) Quick Revision (1 Minute)


 Destructor destroys objects and frees memory (general OOP idea).
 Java does not have destructors like C++.
 Java uses Garbage Collector to free memory automatically.
 Objects are collected when they become unreachable (e.g., set to null).

8) Important Exam / Viva Questions


 Define Destructor. Why does Java not support destructors like C++?
 Differentiate between Constructor and Destructor (at least three points).
 What is Garbage Collection in Java? How is it related to destructors?

You might also like