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

Java Constructors and Destructors Explained

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

Java Constructors and Destructors Explained

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

Java Constructor and Destructor (Simplified Notes)

■ Constructor in Java

Definition: A constructor in Java is a special method that is automatically called when an


object of a class is created. It is used to initialize objects.

Key Points

• Name must be same as class name


• No return type
• Called automatically
• Can be overloaded
• Default constructor provided if not defined

Types of Constructors

1. Default Constructor
<font color="red"><b>class</b></font> Demo {
Demo() { <font color='green'>// default constructor</font>
[Link]<font color="red"><b>int</b></font>ln("Default Constructor called");
}
}

<font color="red"><b>public</b></font> <font color="red"><b>class</b></font> Test {


<font color="red"><b>public</b></font> <font color="red"><b>static</b></font> <font
Demo d = <font color="red"><b>new</b></font> Demo(); <font color='green'>// cons
}
}
2. Parameterized Constructor
<font color="red"><b>class</b></font> Student {
<font color="red"><b>String</b></font> name;
<font color="red"><b>int</b></font> age;

Student(<font color="red"><b>String</b></font> n, <font color="red"><b>int</b></font


name = n;
age = a;
}

<font color="red"><b>void</b></font> display() {


[Link]<font color="red"><b>int</b></font>ln(name + " " + age);
}
}

<font color="red"><b>public</b></font> <font color="red"><b>class</b></font> Test {


<font color="red"><b>public</b></font> <font color="red"><b>static</b></font> <font
Student s1 = <font color="red"><b>new</b></font> Student("Alia", 22);
[Link]();
}
}
3. Copy Constructor
<font color="red"><b>class</b></font> Student {
<font color="red"><b>String</b></font> name;
<font color="red"><b>int</b></font> age;
Student(<font color="red"><b>String</b></font> n, <font color="red"><b>int</b></font
name = n;
age = a;
}

<font color='green'>// copy constructor</font>


Student(Student s) {
name = [Link];
age = [Link];
}

<font color="red"><b>void</b></font> display() {


[Link]<font color="red"><b>int</b></font>ln(name + " " + age);
}
}

<font color="red"><b>public</b></font> <font color="red"><b>class</b></font> Test {


<font color="red"><b>public</b></font> <font color="red"><b>static</b></font> <font
Student s1 = <font color="red"><b>new</b></font> Student("Alia", 22);
Student s2 = <font color="red"><b>new</b></font> Student(s1); <font color='gree
[Link]();
}
}

■ Destructor in Java

Definition: There is no destructor in Java like C++. Java uses a Garbage Collector (GC)
that automatically destroys unused objects and frees memory. The finalize() method is
used as an alternative.

Key Points

• Constructor creates objects


• Destructor destroys objects
• Garbage Collector runs automatically
• finalize() used for cleanup
• finalize() cannot be overloaded and has no parameters
Example: finalize() method
<font color="red"><b>public</b></font> <font color="red"><b>class</b></font> Demo {
<font color="red"><b>public</b></font> <font color="red"><b>static</b></font> <font
Demo d = <font color="red"><b>new</b></font> Demo(); <font color='green'>// obj
d = null; <font color='green'>// object ready for GC</font>
[Link](); <font color='green'>// request garbage collection</font>
[Link]<font color="red"><b>int</b></font>ln("Main method finished");
}

<font color="red"><b>protected</b></font> <font color="red"><b>void</b></font> final


[Link]<font color="red"><b>int</b></font>ln("Object is garbage collected")
}
}

■ Constructor vs Destructor

Constructor
• Initializes an object
• Called when object is created
• Can be overloaded
• Can take arguments

Destructor
• Destroys object / frees memory
• Called before object is destroyed
• Cannot be overloaded
• Cannot take arguments

You might also like