Introduction to Constructor and Destructor in Java
The following article, Constructor, and Destructor in Java, provides a detailed outline for the creation of constructor and destructor in Java. Every Programming language has this concept called constructor and destructor. Java is an object-oriented programming language. If you know the object-oriented concepts, it will be beneficial to understand them more clearly. A constructor is something that initializes objects, and destructors are to destroy that initialization. Java has automatic garbage collection, which used the mark and sweep’s algorithm.
What is Constructor and Destructor in Java?
A constructor is used to initialize a variable that means it allocates memory for the same A constructor is nothing but automatic initialization of the object. Whenever the program creates an object at that time constructor, is gets called automatically. You don’t need to call this method explicitly. Destructor is used to free that memory allocated during initialization. Generally, in java, we don’t need to call the destructor explicitly. Java has a feature of automatic garbage collection.
Why do we Need a Constructor and Destructor in Java?
Constructor and destructor are mostly used to handle memory allocation and de-allocation efficiently. Constructor and destructor do a very important role in any programming language of initializing and destroying it after use to free up the memory space.
How Constructor and Destructor Works in Java
A constructor is just a method in java, which has the same name as the class name. The constructor method does not have any return type to it.
Look at the following example for more clarity:
class Employee {
Employee() {
}
}
If you see in the above example, we have not given any return type like int or void to the method, which has the same name as a class name.
It is mainly used to initialize the object. When we are creating an object of a class, at that time, constructor gets invoked.
It will be more clear with the following code snippet.
How to create Constructors and Destructors in java?
Look at the following example
class Employee {
Employee() { //This is constructor. It has same name as class name.
System.out.println("This is the default constructor");
}
}
Types of Constructor
There are two types of constructors; depending upon the type, we can add and remove variables.
- Default Constructor
- Parameterized Constructor
With this, we are also going to see constructor overloading.
1. Default Constructor
This is the one type of constructor. By default, without any parameters, this constructor takes place. This constructor does not have any parameters in it.
Example:
class Abc{
Abc(){
System.out.println("This is the example of default constructor.");
}
}
2. Parameterized Constructor
As the name suggests, the parameterized constructor has some parameters or arguments at the time of initializing the object.
Example:
import java.util.*;
class Square{
int width,height;
Square( int a , int b){
width = a;
height = b;
}
int area(){
return width * height;
}
}
class Cal{
public static void main(String[] args){
{
Square s1 = new Square(10,20);
int area_of_sqaure = s1.area();
System.out.println("The area of square is:" + area_of_sqaure);
}
}
}
Output:
Now, it is time to talk about constructor overloading in java. This means that having multiple constructors with different parameters. So with this, each constructor can do different tasks. Sometimes as per the requirement, we need to initialize constructors in different ways.
Example
import java.util.*;
public class Main{
String name;
int quantity;
int price;
Main( String n1, int q1, int p1){
name = n1;
quantity = q1;
price = p1;
}
Main( String n2, int p2){
name = n2;
price = p2;
quantity = price/10;
}
void display(){
System.out.println("Product Name "+ name);
System.out.println("Product quantity is "+ quantity);
System.out.println("Product price is "+ price);
}
public static void main(String[] args){
Main product1;
product1 = new Main("Dates",500,50);
product1.display();
product1 = new Main("cashu",800);
product1.display();
}
}
Output:
Try out the above program, and you will be clear what exactly happening with constructor overloading.
Destructor
Before start talking about the destructor, let me tell you there is no destructor in java. Destructor is in C++ programming language. If we are talking about java, then java has a feature called automatic garbage collector. Which free the dynamically allocated memory when there is no use. This concept is very important, and you can explore more about this garbage collection in java.
- Java uses the garb collection technique for memory allocation automatically.
- There is no need to explicit use of destructors like C++.
- For allocating memory in java, we do not have a malloc function like in C programming.
- The new operator in java does the same process of allocating memory.
- new keyword allocates memory space for an object on heap memory.
- At the time of program execution, a new keyword allocates some memory space for the object. End-user need to worry about this as the program handles memory allocation. At the time when the object used in programs done with the work, the memory used for the object is utilized for another task. This process of utilizing memory efficiently is the job of garbage collection in java.
Let’s talk about destructor then. As we know, there is no destructor in java as it has finalize() method to do so. The following are some of the key points to be noted.
Finalize() Methods
- Finalize method is work like a destructor and opposite of a constructor, as we have seen earlier.
- Generally, the finalize method is used to remove the object.
- For using this method, we have to define this method in java explicitly.
- The finalize method starts working after garbage collection done with its work.
- This simply means that after freeing memory space by deallocating memory space from objects, there are chances that memory utilization still there with other things like fonts etc., to delete that memory space or to frees up that space, we make use of finalize() method.
Conclusion
Constructor and destructor(garbage collection in java) are very important things to get clear in any programming language as this is the start where you can actually get how things are done in the background to manage memory space.
Recommended Articles
This is a guide to Constructor and Destructor in Java. Here we discuss the introduction to Constructor and Destructor, Why do we need it and how does it work in java, along with an example. You may also look at the following articles to learn more –
- Bit Manipulation in Java
- What is Java Interface?
- Constructor and Destructor in C++
- Destructor in PHP
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses