What Is Constructor
A constructor in Java is a special method that is automatically called when an object of a class is created. Its main purpose is to initialize the object's data.
Rules:
Constructor name must be the same as the class name.
It does not have a return type (not even void).
It is called automatically when you create an object using the new keyword.
A class can have multiple constructors (constructor overloading).
Example:
class Student {
String name;
int age;
Student() {
name = "Vignesh";
age = 22;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Output:
Name: Vignesh
Age: 22
When we write Student s1 = new Student(); the constructor Student() is automatically called.
So the flow is
new Student() => Student() constructor => name = "Vignesh"
age = 22 => s1 object is initialized
Constructor Overloading
Constructor overloading means having multiple constructors in the same class with different parameter lists.
For example
class Student {
String name;
int age;
// Constructor 1
Student() {
name = "Unknown";
age = 0;
}
// Constructor 2
Student(String n) {
name = n;
age = 0;
}
// Constructor 3
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("Vignesh");
Student s3 = new Student("Arun", 21);
s1.display();
s2.display();
s3.display();
}
}
Output
Unknown 0
Vignesh 0
Arun 21
Here we have three constructors
Student()
Student(String n)
Student(String n, int a)
They have the same name, but their parameter lists are different,
That's constructor overloading.
this Keyword
Once you understand constructors, this becomes much easier.
The this keyword refers to the current object.
The most common use is when the instance variable and constructor parameter have the same name.
Without this
class Student {
String name;
int age;
Student(String name, int age) {
name = name;
age = age;
}
}
This is a problem because Java cannot distinguish the instance variable from the parameter in the way we intend. Both sides refer to the parameter.
So we use this
this with Constructor
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(this.name);
System.out.println(this.age);
}
public static void main(String[] args) {
Student s1 = new Student("Vignesh", 22);
s1.display();
}
}
Output
Vignesh
22
Understand this line
this.name = name;
There are two names.
this.name = name
↑ ↑
instance variable parameter
of current object
So here
this.name = name;
Store the parameter name into the current object's name variable.
this Means Current Object
Consider these Two Objects
Student s1 = new Student("Vignesh", 22);
Student s2 = new Student("Arun", 21);
For s1:
this → s1
For s2:
this → s2
So Therefore here:
this.name
name of the current object
this is a reference variable in Java that refers to the current object. It is commonly used to distinguish instance variables from parameters
References
https://www.geeksforgeeks.org/java/constructors-in-java/
https://www.w3schools.com/java/java_constructors.asp
https://www.programiz.com/java-programming/constructors
Top comments (0)