Constructors
About
A constructor in Java is a special method used to initialize objects. It is automatically called when an object is created. The main purpose of a constructor is to assign initial values to the object's fields.
Characteristics of a Constructor
Same Name as Class
A constructor must have the same name as the class.
No Return Type
Unlike methods, constructors do not have a return type (not even void
).
Automatically Called
It executes when an object is created using new
.
Can Have Parameters
Just like methods, constructors can accept parameters to initialize object values.
Can Have Multiple Constructors (Constructor Overloading)
Java allows multiple constructors with different parameter lists (overloading).
Can have access modifiers
Constructors in Java can have access modifiers just like methods and fields. The access modifier of a constructor determines where it can be accessed.
Access Modifiers for Constructors
public
The constructor is accessible from anywhere in the project.
private
The constructor is accessible only within the same class (used in Singleton pattern).
Prevents direct object creation from outside the class.
Used in Singleton Pattern to ensure only one instance exists.
protected
The constructor is accessible within the same package and in subclasses.
Allows access within the same package and in subclasses.
Used when creating an abstract class with a protected constructor.
default (no modifier)
The constructor is accessible only within the same package.
Example
Types of Constructors in Java
1. Default Constructor (No-Argument Constructor)
A constructor without parameters that initializes default values.
2. Parameterized Constructor
A constructor that accepts arguments to initialize object fields.
Usage:
3. Copy Constructor
A constructor that copies values from another object.
Usage:
4. Private Constructor
A constructor that is private to prevent object creation. Used in Singleton Design Pattern.
Usage:
5. Constructor Overloading
A class can have multiple constructors with different parameters.
Usage:
Can we have Destructors in Java?
Java does not have destructors like C++, but it provides finalization mechanisms for resource cleanup.
Why No Destructors in Java?
Garbage Collection Handles Memory Cleanup:
In Java, objects are automatically garbage collected when they are no longer referenced.
Unlike C++, where destructors explicitly free memory, Java depends on the JVM's garbage collector.
No Explicit Object Destruction:
In C++, destructors (
~ClassName()
) are used for manual cleanup of allocated memory and resources.In Java, there is no direct equivalent because objects are deallocated by the JVM's garbage collector, making explicit destruction unnecessary.
Alternatives to Destructors in Java
1. finalize()
(Deprecated)
finalize()
(Deprecated)Java provided a
finalize()
method inObject
class as a cleanup mechanism, but it was unreliable and has been deprecated in Java 9.Called before garbage collection, but not guaranteed to execute immediately or at all.
Why Avoid finalize()
?
Unpredictable execution (depends on garbage collection timing).
Causes performance overhead.
Might not run before program exit.
2. try-with-resources
(Preferred Approach)
try-with-resources
(Preferred Approach)For managing resources like files, database connections, sockets, etc., Java provides the try-with-resources mechanism. Classes that implement AutoCloseable
or Closeable
can be automatically closed when the block exits.
3. Explicit close()
Method
close()
MethodFor manual cleanup, define a close()
method and call it explicitly.
Recommended for managing non-memory resources like files, sockets, database connections.
Last updated
Was this helpful?