In Java, a constructor is a special method used to initialize objects. Java supports three common types of constructors:
- Default Constructor (No arguments)
- Parameterized Constructor (Accepts arguments)
- Copy Constructor (Creates a new object by copying another object)
1. Default Constructor
A default constructor is a constructor without parameters. If a class does not define any constructor, Java automatically provides a default constructor.
✅ Example: Default Constructor
class Animal {
// Default constructor (no parameters)
Animal() {
System.out.println("An Animal is created!");
}
}
public class DefaultConstructorExample {
public static void main(String[] args) {
Animal a = new Animal(); // Calls the default constructor
}
}
✅ Output:
An Animal is created!
Default Behavior:
- If no constructor is defined, Java provides: javaCopyEdit
Animal() {
super(); // Calls Object class constructor
}
- Instance variables get default values:
int
→0
boolean
→false
String
& objects →null
2. Parameterized Constructor
A parameterized constructor accepts arguments to initialize an object with custom values.
✅ Example: Parameterized Constructor
class Person {
String name;
int age;
// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
public class ParameterizedConstructorExample {
public static void main(String[] args) {
Person p1 = new Person("Alice", 25); // Constructor with parameters
p1.display();
}
}
✅ Output:
Alice is 25 years old.
Key Points:
- Allows custom initialization.
- Java does not provide a default constructor if any constructor is explicitly defined.
- You can define multiple constructors (overloading).
3. Copy Constructor
A copy constructor creates a new object by copying another object’s values.
✅ Example: Copy Constructor
class Book {
String title;
// Constructor
Book(String title) {
this.title = title;
}
// Copy Constructor
Book(Book other) {
this.title = other.title;
}
void display() {
System.out.println("Book Title: " + title);
}
}
public class CopyConstructorExample {
public static void main(String[] args) {
Book b1 = new Book("Java Programming");
Book b2 = new Book(b1); // Copy constructor
b1.display(); // Java Programming
b2.display(); // Java Programming
}
}
✅ Output:
Book Title: Java Programming
Book Title: Java Programming
Key Points:
- Creates a new object with the same values.
- Useful for cloning objects without modifying the original.
- Prevents modifying shared references in some cases.
4. Comparison Table
Feature | Default Constructor | Parameterized Constructor | Copy Constructor |
---|---|---|---|
Definition | No parameters, initializes default values | Takes parameters to set values | Copies values from another object |
Provided by Java? | ✅ Yes (if no constructor is defined) | ❌ No, must be explicitly written | ❌ No, must be explicitly written |
Custom Values? | ❌ No | ✅ Yes | ✅ Yes (copied from another object) |
Example | Person() { } | Person(String name, int age) {} | Person(Person other) {} |
Use Case | When no specific initialization is needed | When initialization values should be specified | When copying an object |
5. Can We Have Multiple Constructors in One Class?
Yes! Constructor overloading allows defining multiple constructors with different parameters.
✅ Example: Combining All Three Constructors
class Employee {
String name;
int age;
// Default Constructor
Employee() {
this.name = "Unknown";
this.age = 0;
}
// Parameterized Constructor
Employee(String name, int age) {
this.name = name;
this.age = age;
}
// Copy Constructor
Employee(Employee other) {
this.name = other.name;
this.age = other.age;
}
void display() {
System.out.println("Employee: " + name + ", Age: " + age);
}
}
public class ConstructorOverloadingExample {
public static void main(String[] args) {
Employee e1 = new Employee(); // Default Constructor
Employee e2 = new Employee("Bob", 30); // Parameterized Constructor
Employee e3 = new Employee(e2); // Copy Constructor
e1.display(); // Employee: Unknown, Age: 0
e2.display(); // Employee: Bob, Age: 30
e3.display(); // Employee: Bob, Age: 30
}
}
✅ Demonstrates constructor overloading!
🚀 Now, we can create employees in multiple ways!
6. Summary
Constructor Type | Definition | Best For |
---|---|---|
Default Constructor | No parameters, initializes default values | Simple object creation |
Parameterized Constructor | Takes arguments to set values | Custom initialization |
Copy Constructor | Duplicates an existing object | Cloning, avoiding modification of the original |
🚀 Best Practice:
- Use default constructors when no special initialization is required.
- Use parameterized constructors when specific values are needed.
- Use copy constructors to create independent copies of objects.