In Java, a constructor is a special method used to initialize objects. It is called automatically when an object is created using the new keyword.
Key Characteristics
- A constructor has the same name as the class.
- It doesn’t have a return type, not even
void. - It can be overloaded, meaning a class can have multiple constructors with different parameters.
- If no constructor is defined, Java provides a default constructor automatically.
Types of Constructors in Java
Java supports three types of constructors:
1. Default Constructor
- A constructor with no parameters.
- Automatically created by the compiler if no constructor is explicitly defined in the class.
- Initializes variables with default values (like
0,null,false, etc.).
2. No-Argument Constructor
- A constructor explicitly defined by the programmer without parameters.
- Often used to assign custom default values during object creation.
3. Parameterized Constructor
- A constructor that accepts one or more arguments.
- Used to initialize an object with custom values passed at the time of object creation.
1️⃣ Default Constructor
A default constructor is automatically provided by Java if no constructor is defined by the programmer. It initializes objects with default values.
Key Characteristics:
- It takes no parameters.
- It is invisible in the code — automatically generated by the Java compiler.
- It initializes all instance variables with their default values:
int,short,byte,long→0float,double→0.0char→'\u0000'(null character)boolean→false- Object references →
null
- You won’t see this constructor unless you write your own — then Java doesn’t add it.
Example
class Student {
int id;
String name;
// No constructor defined, Java provides a default one
void display() {
System.out.println(id + " " + name); // default values
}
public static void main(String[] args) {
Student s1 = new Student(); // default constructor is called
s1.display();
}
}
Output
0 null
2️⃣ No-Argument Constructor
This is a constructor written by the programmer that does not take any parameters but may assign default values.
Unlike the default constructor (which is generated automatically by Java), the no-arg constructor is defined manually, and you can write any custom logic or default assignments inside it.
Key Characteristics:
- Written explicitly by the programmer.
- Takes no arguments/parameters.
- Can be used to initialize object fields with custom default values.
- Useful when we need object creation with fixed or hardcoded values.
Example
class Car {
String model;
int year;
// No-argument constructor
Car() {
model = "BMW";
year = 2023;
}
void display() {
System.out.println(model + " " + year);
}
public static void main(String[] args) {
Car c1 = new Car();
c1.display();
}
}
Output:
BMW 2023
How the No-Argument Constructor Works:
1. Car c1 = new Car();
➤ This line creates a new Car object and automatically calls the no-argument constructor.
2. Inside the Constructor:
Car() { model = "BMW"; year = 2023; }
➤ The constructor assigns default values:
model="BMW"year=2023
3. c1.display();
➤ Calls the display() method, which prints the values initialized by the constructor.
3️⃣ Parameterized Constructor
A parameterized constructor takes arguments to initialize the object with custom values.
Instead of assigning fixed or default values (like in no-arg constructors), parameterized constructors allow you to pass different values for each object.
Key Characteristics:
- You define it manually in your class.
- It requires arguments when creating an object.
- Useful when different objects need different initial values.
- It allows constructor overloading (you can have both parameterized and no-arg constructors).
Syntax
ClassName(type1 var1, type2 var2, ...) {
// initialization code
}
Example
class Employee {
String name;
int age;
// Parameterized constructor
Employee(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " is " + age + " years old");
}
public static void main(String[] args) {
Employee e1 = new Employee("Alice", 25);
Employee e2 = new Employee("Bob", 30);
e1.display();
e2.display();
}
}
Output
Alice is 25 years old
Bob is 30 years old
How It Works:
- When
new Car("BMW", 2023)is called:- The parameterized constructor is triggered.
"BMW"is assigned tomodel.2023is assigned toyear.
- Each object (
c1,c2) has its own custom values because different arguments were passed.
Benefits of Parameterized Constructors:
- Enables object customization at creation.
- Supports constructor overloading.
- Avoids repetitive code (no need to call setters later).
Summary
| Type | Parameters | Automatically Provided | Custom Initialization |
|---|---|---|---|
| Default Constructor | ❌ | ✅ Yes | ❌ |
| No-Arg Constructor | ❌ | ❌ You define it | ✅ Yes |
| Parameterized Constructor | ✅ | ❌ | ✅ Yes |