What is a Constructor?
A constructor is a special method that is automatically called whenever an object of a class is created.
Unlike normal methods, a constructor:
- Has the same name as the class.
- Does not have a return type (not even
void). - Is executed automatically when an object is created using the
newkeyword.
Syntax
class ClassName {
ClassName() {
// Constructor body
}
}
Why Do We Need Constructors?
Constructors help us:
- Initialize object variables.
- Reduce repetitive code.
- Ensure objects start with valid values.
- Improve code readability.
Example 1: Default Constructor
class Student {
Student() {
System.out.println("Student object created successfully.");
}
public static void main(String[] args) {
Student s1 = new Student();
}
}
Output
Student object created successfully.
Explanation
When the statement
Student s1 = new Student();
is executed,
Java automatically calls the constructor
Student()
which prints the message.
Example 2: Constructor Initializing Variables
class Product {
String name;
int price;
Product() {
name = "Rice";
price = 1500;
}
void display() {
System.out.println("Product Name : " + name);
System.out.println("Price : " + price);
}
public static void main(String args[]) {
Product p1 = new Product();
p1.display();
}
}
Output
Product Name : Rice
Price : 1500
Explanation
The constructor assigns default values to the object variables. Whenever a new object is created, these values are automatically initialized.
Types of Constructors
Java mainly has two types of constructors.
1. Default (No-Argument) Constructor
A constructor that does not take any parameters.
Example:
Student() {
}
2. Parameterized Constructor
A constructor that accepts parameters.
Example:
Student(String name, int age) {
}
Parameterized constructors allow us to initialize objects with different values.
What is Constructor Overloading?
Constructor Overloading means having multiple constructors in the same class with different parameter lists.
Each constructor performs a different type of object initialization.
Java determines which constructor to call based on the arguments passed while creating the object.
Example of Constructor Overloading
class Employee {
String name;
int age;
Employee() {
name = "Unknown";
age = 0;
}
Employee(String n) {
name = n;
age = 0;
}
Employee(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String args[]) {
Employee e1 = new Employee();
Employee e2 = new Employee("Athithya");
Employee e3 = new Employee("Athithya", 20);
e1.display();
e2.display();
e3.display();
}
}
Output
Unknown 0
Athithya 0
Athithya 20
How Constructor Overloading Works
When these statements execute,
Employee e1 = new Employee();
Java calls
Employee()
When
Employee e2 = new Employee("Athithya");
Java calls
Employee(String n)
When
Employee e3 = new Employee("Athithya", 20);
Java calls
Employee(String n, int a)
Java chooses the constructor based on the number and type of arguments provided.
Advantages of Constructor Overloading
- Provides multiple ways to initialize objects.
- Makes programs more flexible.
- Improves code readability.
- Reduces the need for multiple initialization methods.
- Supports object-oriented programming principles.
Difference Between Constructors and Methods
| Constructor | Method |
|---|---|
| Same name as the class | Can have any valid name |
| No return type | Has a return type or void
|
| Called automatically | Called explicitly |
| Used to initialize objects | Used to perform operations |
Top comments (0)