Constructors in Java
Learn how constructors initialize objects and how Java supports default, parameterized, overloaded, and constructor-chaining techniques.
What is a Constructor?
A constructor is a special member of a class that is invoked when an object is created. It is primarily used to initialize the object's state.
A constructor has the same name as its class and does not
have a return type, not even void.
class Student {
Student() {
System.out.println("Constructor called: CIIT Student Successfully Placed 🤓...!");
}
}
Constructor Invocation
Student student = new Student();
When the object is created, the constructor is invoked as part of the object-creation process.
Basic Constructor Example
class Employee {
String name;
Employee() {
name = "Unknown";
}
void display() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
employee.display();
}
}
Unknown
Constructor Rules
- Constructor name must match the class name.
- Constructors do not have a return type.
- Constructors are invoked when objects are initialized.
- Constructors can accept parameters.
- Constructors can be overloaded.
- Constructors are not inherited by subclasses.
Default Constructor
If a class declares no constructor at all, the Java compiler provides a default no-argument constructor.
class Student {
String name;
}
Student student = new Student();
The compiler-generated constructor performs the normal initialization required for object creation, including assigning default values to instance fields.
Important: Compiler-Provided Constructor
The compiler provides a default constructor only when the class declares no constructor.
class Student {
Student(String name) {
// parameterized constructor
}
}
In this case, Java does not automatically add a no-argument constructor.
No-Argument Constructor
A programmer can explicitly define a constructor that accepts no arguments.
class Student {
Student() {
System.out.println("Student created in CIIT training institute ❤️..!");
}
}
Parameterized Constructor
A parameterized constructor accepts values that can be used to initialize object fields.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Creating an Object with Parameters
Student student =
new Student("Samadhan", 22);
System.out.println(student.name);
System.out.println(student.age);
Samadhan
22
Using this in Constructors
The this keyword refers to the current object.
It is commonly used when constructor parameters have the
same names as instance fields.
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
}
Constructor Overloading
A class can contain multiple constructors with different parameter lists.
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(String name) {
this.name = name;
age = 0;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Calling Different Constructors
Student first = new Student();
Student second = new Student("Amit");
Student third = new Student("Rahul", 25);
Constructor Chaining with this()
One constructor can call another constructor in the same
class using this().
class Student {
String name;
int age;
Student() {
this("Unknown", 0);
}
Student(String name) {
this(name, 0);
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Rule for this()
A constructor invocation using this() must be
the first statement in the constructor body.
Student() {
this("Unknown");
}
Calling a Parent Constructor with super()
A subclass constructor can invoke a superclass constructor
using super().
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class Student extends Person {
int age;
Student(String name, int age) {
super(name);
this.age = age;
}
}
Rule for super()
If used explicitly, super() must also be the
first statement in a constructor.
If a constructor does not explicitly invoke another
constructor using this() or super(),
Java inserts an implicit no-argument superclass constructor
invocation when applicable.
Object Initialization Order
Object initialization follows a defined sequence involving superclass initialization, instance initialization, and the selected constructor.
Object creation
↓
Superclass initialization
↓
Subclass instance initialization
↓
Constructor body
↓
Object ready for use
Instance Initialization Block
Java supports instance initializer blocks. They execute during object initialization before the constructor body.
class Demo {
{
System.out.println("Initializer block");
}
Demo() {
System.out.println("Constructor");
}
}
Private Constructors
A constructor can be declared private. This
prevents code outside the class from directly creating
instances through that constructor.
class Utility {
private Utility() {
}
}
Private constructors are commonly used in utility classes and certain controlled-instance designs.
Copy Constructor Pattern
Java does not provide a special built-in copy-constructor feature, but developers can create a constructor that accepts another object of the same class.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
Student(Student other) {
this.name = other.name;
this.age = other.age;
}
}
Constructor vs Method
| Feature | Constructor | Method |
|---|---|---|
| Name | Same as class name. | Can have any valid method name. |
| Return Type | No return type. | Must declare a return type or void. |
| Purpose | Object initialization. | Performs behavior or computation. |
| Invocation | Associated with object creation. | Called explicitly. |
| Overloading | Supported. | Supported. |
Practical Example: Bank Account
class BankAccount {
String accountHolder;
double balance;
BankAccount(String accountHolder) {
this(accountHolder, 0.0);
}
BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
void display() {
System.out.println(
accountHolder + " : " + balance
);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account =
new BankAccount("Samadhan", 5000.0);
account.display();
}
}
Constructor Best Practices
- Initialize the object into a valid and meaningful state.
- Keep constructors reasonably small.
- Avoid performing slow or unrelated operations inside constructors.
- Use constructor chaining to reduce duplicate initialization logic.
- Validate important constructor arguments when required.
- Use appropriate access modifiers to control object creation.
Interview Questions
this() invokes another constructor
in the same class and must be the first statement
in the constructor.
super() invokes a superclass
constructor.
Summary
Constructors initialize Java objects when they are
created. They can be no-argument or parameterized,
overloaded, chained with this(), and used
with super() to initialize inherited
state. Good constructor design helps ensure objects
start in a valid state.