Introduction
Constructors are called whenever an instance of a given class is created. Finalizers are used to destroy the object created using constructors.
A Constructor method is a special kind of method that determines how an object is finalized when created. They have the same name as the class and do not have any return type. Finalizer method functions in contradiction to the constructor method. Finalizers are called just before the object is garbage collected and its memory is reclaimed. The finalizer method is represented by finalize().
Constructor
When the keyword new is used to create an instance of a class, Java allocates memory for the object, initializes the instance variables, and calls the constructor methods. Every class has a default constructor that does not take any arguments and its body of it does not have any statements. Constructors can also be overloaded.
The example below describes the constructor definition, passing of arguments, and changing of values.
class Cons {
int i;
int j;
Cons(int a, int b) {
i = a;
j = b;
}
void print() {
System.out.println("The Addition of " + i + " and " + j + " gives " + (i + j));
}
public static void main(String args[]) {
Cons c;
c = new Cons(10, 10);
c.print();
System.out.println();
c = new Cons(50, 50);
c.print();
System.out.println();
}
}
![Constructor Output]()
The constructor passes arguments in the above code. It also indicates the print method used to display the values of the constructor arguments.
Calling Another Constructor
A Constructor can be called from another constructor. When a constructor is written, the functionalities of another defined constructor can be used. A call to the constructor can be made from the constructor currently being defined. To call a constructor defined in the current class use.
this(arg1, arg2, arg3…)
The arguments to this are the arguments to the constructor. An example illustrates the concept.
class point
{
int x, y;
point(int x, int y)
{
this.x = x;
this.y = y;
}
point()
{
this(-1, -1);
}
public static void main(String args[])
{
point p = new point();
System.out.println("x=" + p.x + ",y=" + p.y);
p = new point(10, 10);
System.out.println("x=" + p.x + ", y=" + p.y);
}
}
Output
![Calling Another Constructor]()
In this example, the second constructor calls the first constructor and initializes the instance variable.
Finalizer
Finalizer method functions in contradiction to the constructor method. Finalizers are called just before the object is garbage collected and its memory is reclaimed. The finalizer method is represented by finalize (). The Object class defines a default finalizer method, which does nothing, To create a finalizer method for the classes, the finalize () method can be overridden.
protected void finalize ()
{
<set of statements>
}
Any cleaning required may be included inside the body of the finalize () method. This method can be called at any point of time. Calling finalize () need not trigger an object to be garbage collected. Removing references to an object will cause it to be marked for deletion. Finalizer methods are best used for optimizing the removal of an object.
Source code
class DemoFinalizer {
DemoFinalizer() {
System.out.println("Constructor called: Object is created.");
}
@Override
protected void finalize() {
System.out.println("Finalizer called: Object is about to be garbage collected.");
}
public static void main(String[] args) {
DemoFinalizer obj = new DemoFinalizer();
obj = null;
System.gc();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("Main method ends.");
}
}
Output
![Finalizer]()
You're seeing this warning because finalize() has been deprecated and marked for removal in newer versions of Java, specifically. This means that the finalize() method in the Object class has been deprecated since Java 9. It is marked for removal in future versions (such as Java 18+), because it's unreliable, unpredictable, and inefficient. The Java community no longer recommends using finalize() for cleanup tasks like releasing resources or memory.
Use java.lang.ref.Cleaner (available since Java 9) or try-with-resources for safe and deterministic resource cleanup.
Source Code
import java.lang.ref.Cleaner;
class DemoFinalizer {
private static final Cleaner cleaner = Cleaner.create();
static class State implements Runnable {
@Override
public void run() {
System.out.println("Cleaner called: Object is being cleaned up.");
}
}
private final Cleaner.Cleanable cleanable;
DemoFinalizer() {
System.out.println("Constructor called: Object is created.");
cleanable = cleaner.register(this, new State());
}
public static void main(String[] args) {
DemoFinalizer obj = new DemoFinalizer();
obj = null;
System.gc();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("Main method ends.");
}
}
Output
![Finalizer]()
But Why is finalize() being removed?
- It causes unpredictable behavior — you don’t know when or if it will run.
- It can cause memory leaks or delay memory release.
- It slows down garbage collection.
- Alternatives like Cleaner are more efficient and safe.
Summary
This article explains the role of constructors and finalizers in Java, focusing on object creation and destruction processes. A constructor is a special method used to initialize objects when a class instance is created. It shares the class name, has no return type, and can be overloaded. The document provides Java code examples demonstrating how constructors can pass arguments and invoke other constructors using the this() keyword.
In contrast, a finalizer finalize() is used for cleanup before an object is garbage collected. However, the finalize() method is now deprecated (since Java 9) due to its inefficiency, unpredictability, and potential to cause memory leaks. As a modern alternative, the document introduces the use of the Cleaner class from java.lang.ref for resource cleanup, which is safer and more reliable. Example code shows how to register an object with the Cleaner for proper resource management.
This article concludes by recommending developers avoid finalize() and adopt Cleaner or try-with-resources for better memory handling and cleanup.