🔧 Constructors
Learn about default, parameterized, overloaded, and copy constructors in Java.
What is a Constructor?
A constructor in Java is a special method used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
Constructor vs Method
| Constructor | Method |
|---|---|
| Used to initialize an object state. | Used to exhibit behavior of an object. |
| Must not have a return type. | Must have a return type. |
| Invoked implicitly when an object is created. | Invoked explicitly using the object reference. |
| Name must match the class name exactly. | Can have any valid identifier name. |
Types of Constructors
1. Default Constructor
If you do not explicitly define a constructor, the Java compiler automatically creates a no-argument constructor called the default constructor. It initializes member variables to their default values (e.g., 0 for integers, null for objects).
2. Parameterized Constructor
A parameterized constructor accepts arguments, allowing you to initialize objects with specific values at the time of creation.
public class Person { String name; int age; // Default Constructor (No arguments) public Person() { this.name = "Unknown"; this.age = 0; } // Parameterized Constructor public Person(String n, int a) { this.name = n; this.age = a; } public void display() { System.out.println(name + " is " + age + " years old."); } public static void main(String[] args) { Person p1 = new Person(); // Calls default constructor Person p2 = new Person("Alice", 30); // Calls parameterized constructor p1.display(); p2.display(); } }
Alice is 30 years old.
Constructor Overloading
Just like methods, constructors can be overloaded. Constructor overloading is the practice of having more than one constructor in a class with different parameter lists (different number of parameters, different types, or both).
public class Rectangle { int length; int width; // Constructor with no parameters public Rectangle() { length = 1; width = 1; } // Constructor with one parameter (Square) public Rectangle(int side) { length = side; width = side; } // Constructor with two parameters public Rectangle(int l, int w) { length = l; width = w; } public int getArea() { return length * width; } public static void main(String[] args) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(5); Rectangle r3 = new Rectangle(4, 6); System.out.println("Area 1: " + r1.getArea()); System.out.println("Area 2: " + r2.getArea()); System.out.println("Area 3: " + r3.getArea()); } }
Area 2: 25
Area 3: 24
Copy Constructor
A copy constructor is used to create a new object using an existing object of the same class. Java does not provide a default copy constructor like C++, so you must write it yourself.
class Book { String title; String author; // Regular constructor public Book(String t, String a) { title = t; author = a; } // Copy constructor public Book(Book b) { title = b.title; author = b.author; } public void display() { System.out.println(title + " by " + author); } } public class CopyConstructorDemo { public static void main(String[] args) { Book book1 = new Book("1984", "George Orwell"); Book book2 = new Book(book1); // Using copy constructor book1.display(); book2.display(); } }
Constructor Chaining with this()
Constructor chaining is the process of calling one constructor from another with respect to the current object. This is done using the this() keyword. It helps to avoid duplicate code when you have overloaded constructors.
public class ConstructorChaining { int x, y, z; public ConstructorChaining() { this(0); // Calls constructor with 1 parameter System.out.println("Default constructor called."); } public ConstructorChaining(int value) { this(value, value); // Calls constructor with 2 parameters System.out.println("Single parameter constructor called."); } public ConstructorChaining(int val1, int val2) { this.x = val1; this.y = val2; this.z = 10; System.out.println("Double parameter constructor called."); } public static void main(String[] args) { ConstructorChaining obj = new ConstructorChaining(); } }
Single parameter constructor called.
Default constructor called.
Private Constructors
A constructor can be declared private. If a class has a private constructor, you cannot create an object of that class from outside the class. This is commonly used in the Singleton Design Pattern to restrict instantiation to a single object, or in utility classes containing only static methods.