SY-4-b Easy program on constructor and destructor in Java

Program on constructor and destructor in Java

Write a Java program that demonstrates the use of constructor and destructor.

Constructor In Java

Constructor is use to initialize objects. We can accept the values for variables during object creation.

  • Constructor method name is exact same as the class name.
  • There is no need of return type for constructor.
  • Constructor gets call when object is instantiated.

Click here for more information.

Destructor In Java

In java there is not such method as destructor, which we can call. Java provides its alternative named “finalize()“.

Java uses Garbage Collector instead of Destructor. Garbage Collector deletes the unused objects from memory to make some space in memory. The garbage collector runs on the JVM itself. Garbage Collector runs finalize method hence we write our code in finalize method.

Suppose we have created reference variable for database, sockets, etc. and  we want to destroy these reference variable once we close the program. We can write closing code in finalize method which will get executed by Garbage Collector.

Java Constructor and Destructor program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class DestructorDemo
{
	public static void main(String[] args)
	{
		while(true)
		{
			new Itvoyagers();
		}
	}
}

class Itvoyagers
{
	public Itvoyagers()
	{
		System.out.println(" This is constructor for Itvoyagers class store at : "+this);
	}

	protected void finalize()
	{
		System.out.println(" This is finalize method for Itvoyagers class store at : "+this);
	}
}

Output

While loop go on and on and it will start instantiating object. Object will get saved in Heap memory once JVM will run Garbage Collector to free some memory. Hence if you run following program you have to wait till Garbage Collector runs.

Garbage Collector will execute finalize method hence we can see finalize method running in between without even calling.

garbage collector in java itvoyagers

Call Garbage Collector

We can call Garbage Collector explicitly using “System.gc()”.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class DestructorDemo
{
	public static void main(String[] args)
	{
		new Itvoyagers();
		System.gc();
	}
}

class Itvoyagers
{
	public Itvoyagers()
	{
		System.out.println(" This is constructor for Itvoyagers class store at : "+this);
	}

	protected void finalize()
	{
		System.out.println(" This is finalize method for Itvoyagers class store at : "+this);
	}
}

Output

finalize method in java

PRACTICALS/PRACTICE PROGRAM IN Java

SYIT Practical Programs
SY-1-a Easy Java program to print multiplication table
SY-1-b Easy program to print inverted pyramid pattern of stars in Java
SY-1-c Easy Java program to print the area and perimeter of a circle
SY-2-c Easy Java program to reverse a string
SY-3-a Easy Java program to count the letters, spaces, numbers and other characters
SY-3-c Easy to find the smallest and largest element from the array in Java
SY-4-a Easy java program to sort array in ascending and descending order
SY-4-b Easy program on constructor and destructor in Java
SY-4-c Implementation of abstract class in Java in easy way
SY-5-a Easy java program to implement single level inheritance
SY-5-b Easy java program to implement method overriding
SY-5-c Easy java program to implement multiple inheritance
SY-6-a Easy program to create package in Java
SY-6-b easy java program to add two matrices and print the resultant matrix
SY-6-c Java program for multiplying two matrices in easy way
SY-7-a easy java program to implement the vectors
SY-7-c Easy and simple java program to implement multithreading
SY-8-a java program to open a file and display the contents in the console window in easy way
SY-8-b Java program to copy the contents from one file to other file in easy way
SY-9-a Java AWT program to print the factorial for an input value in easy way
SY-9-b Java AWT program to perform various string operations in easy way
SY-9-c Easy java program to implement exception handling
SY-10-a Java AWT program to accept and display student details in easy way
ST-10-b Java program to design a calculator based on AWT application in easy way
SYCS Practical Programs Semester 3
Java program to solve quadratic equations to print the roots of ax2+bx+c=0
Java program to add two matrices
Accept n strings. Sort names in ascending order
Create a package: Animals. In package animals create interface Animal with suitable behaviors. Implement the interface Animal in the same package animals
Demonstrate Java inheritance using extends keyword.
Demonstrate method overloading and method overriding in Java.
Demonstrate creating your own exception in Java.
Using various swing components design Java application to accept a student’s resume. (Design form)
Write a Java List example and demonstrate methods of Java List interface.
Design simple calculator GUI application using AWT components
Write a Java program that takes a number as input and prints its multiplication table upto 10.
Java program to print reverse pyramid
Write a Java program to print the area and perimeter of a circle
SYCS Practical Programs Semester 4
SYCS-5 Java application to demonstrate servlet life cycle in easy way
Practice Program
1 – Best Java program to find factorial of a number
2 – Java program to check if number is Even or Odd
3 – Best Java program to check prime number
4 – Simple and easy Java program to calculate area
5 – Best Java program to convert decimal to binary
6 – Best Java program to implement stack using array
7 – Best Java program to check palindrome number
8 – Best Java program to reverse a number
9 – Easy and simple billing system program in java swing
10 – Easy and simple java program for billing system of tour and travels
More coming soon…

CHECKOUT OTHER RELATED TOPICS

We are aiming to explain all concepts of Java in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment