Java Constructors
A constructor in Java is a special member that is called
when an object is created. It initializes the new object’s state.
It is used to set default or user-defined values for the object's
attributes.
A constructor has the same name as the class.
It does not have a return type, not even void.
It can accept parameters to initialize object properties.
Types of Constructors in Java
There are four types of constructors in Java
Parameterized Construc[Link] in Java
3. Copy Constructor in Java
Unlike other constructors copy constructor is passed with another object which copies the data available
from the passed object to the newly created object.
A copy constructor in a Java class is a constructor that creates an object using another object of the same Java
class. That’s helpful when we want to copy a complex object that has several fields, or when we want to make
a deep copy of an existing object.(According to [Link] 2024)
4. A private constructor is a constructor declared with the
keyword private. This means it cannot be accessed or
called from outside the class.
Why do we use a Private Constructor?
1. To prevent Object Creation
2. Creating Static-Only Classes
3. Singleton Design Pattern
4. Controlling Object Creation