A constructor is a special member of a Java class used to
initialize objects. It is called when an object is created
using the new keyword.
A constructor is similar to a method, but it has the same name as its class and does not have a return type.
class Student {
Student() {
System.out.println("Constructor called");
}
public static void main(String[] args) {
Student s1 = new Student();
}
}
Constructor called
When new Student() creates the object,
Java invokes the constructor.
void.class Car {
Car() {
System.out.println("Car object initialized");
}
}
void Car() defines
a method, not a constructor.
If you do not declare any constructor in a class, Java automatically provides a default constructor. It initializes fields to their default values.
class Student {
String name;
int age;
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.name);
System.out.println(s1.age);
}
}
null
0
Here, Java supplies a default constructor because the class declares no constructor.
A no-argument constructor is a constructor that accepts no parameters. It can be written by the programmer.
class Student {
String name;
int age;
Student() {
name = "Rahul";
age = 20;
}
void display() {
System.out.println(name);
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Rahul
20
The constructor assigns initial values to the object's fields.
A parameterized constructor accepts values that can be used to initialize an object.
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul", 20);
Student s2 = new Student("Priya", 21);
s1.display();
s2.display();
}
}
Rahul 20
Priya 21
Each constructor call initializes a different object using the supplied arguments.
Constructor overloading means defining multiple constructors in the same class with different parameter lists.
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(String n) {
name = n;
age = 18;
}
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Amit");
Student s3 = new Student("Priya", 21);
s1.display();
s2.display();
s3.display();
}
}
Unknown 0
Amit 18
Priya 21
The this keyword refers to the current
object. It is commonly used to distinguish instance
fields from constructor parameters with the same name.
class Employee {
String name;
int salary;
Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
void display() {
System.out.println(name);
System.out.println(salary);
}
public static void main(String[] args) {
Employee e1 = new Employee("Amit", 30000);
e1.display();
}
}
Amit
30000
this.name refers to the object's field,
while name refers to the parameter.
A constructor can call another constructor in the
same class using this().
class Student {
String name;
int age;
Student() {
this("Unknown", 0);
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Rahul", 20);
s1.display();
s2.display();
}
}
Unknown 0
Rahul 20
this() must be the first statement
in the constructor body. Newer Java versions also
have limited flexible-constructor-body rules.
| Constructor | Method |
|---|---|
| Has the same name as its class. | Can have a different name. |
| Has no return type. | Has a return type, including void. |
| Invoked during object creation or through constructor chaining. | Called to perform an operation. |
| Cannot be overridden. | Instance methods can be overridden when the rules permit. |
A private constructor can only be invoked from within its declaring class. It can be useful when a class should control object creation.
class Utility {
private Utility() {
System.out.println("Private constructor");
}
static void showMessage() {
System.out.println("Hello Java");
}
public static void main(String[] args) {
showMessage();
}
}
Hello Java
The program does not create a Utility object. The private constructor prevents direct construction from outside the class.
When a subclass object is created, its constructor also invokes a constructor of its superclass.
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog constructor");
}
public static void main(String[] args) {
Dog d1 = new Dog();
}
}
Animal constructor
Dog constructor
If the superclass has no accessible no-argument
constructor, the subclass must explicitly call an
appropriate superclass constructor using
super(...).
class BankAccount {
String accountHolder;
double balance;
BankAccount(String name, double initialBalance) {
accountHolder = name;
balance = initialBalance;
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
void display() {
System.out.println("Account Holder: " + accountHolder);
System.out.println("Balance: " + balance);
}
public static void main(String[] args) {
BankAccount account =
new BankAccount("Rahul", 1000);
account.deposit(500);
account.display();
}
}
Account Holder: Rahul
Balance: 1500.0
The constructor initializes the account holder and starting balance. The deposit method updates the balance afterward.
this keyword in its constructor.
this().
1. What is the main purpose of a constructor?
2. What is true about a constructor's return type?
3. Which keyword calls another constructor in the same class?