What is a Constructor?
A constructor is a special block of code used to initialize objects in Java.
It is automatically called when we create an object using the new keyword.
Rules of Constructors
- Constructor name must be the same as the class name.
- A constructor doesn't have a return type, not even
void. - It is automatically called when an object is created.
- Constructors can be overloaded.
How to Create a Constructor?
Syntax:
class ClassName { ClassName() { // Constructor body } } Example:
public class Student { Student() { System.out.println("Constructor Called"); } public static void main(String[] args) { Student student1 = new Student(); } } Output:
Constructor Called Here, the constructor is automatically called when the student1 object is created.
Types of Constructors
1) Default Constructor
If we don't create any constructor in a class, Java automatically provides a default constructor.
public class Student { public static void main(String[] args) { Student student1 = new Student(); } } Here, Java provides a default constructor because we haven't declared any constructor.
Note: If we create our own constructor, Java will not provide the default constructor automatically.
2) No-Argument Constructor
A constructor that doesn't accept any parameters is called a no-argument constructor.
public class Student { Student() { System.out.println("Student Created"); } public static void main(String[] args) { Student student1 = new Student(); } } 3) Parameterized Constructor
A constructor that accepts parameters to initialize object variables is called a parameterized constructor.
public class Student { String name; int age; Student(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) { Student student1 = new Student("John", 22); System.out.println(student1.name); System.out.println(student1.age); } } Output:
John 22 Here, the constructor receives values and initializes the instance variables.
What is the this Keyword?
The this keyword refers to the current object.
It is commonly used to differentiate instance variables from constructor parameters when they have the same name.
Student(String name, int age) { this.name = name; this.age = age; } Here:
-
this.namerefers to the instance variable. -
namerefers to the constructor parameter.
Constructor Overloading
Constructor overloading means creating multiple constructors in the same class with different parameter lists.
public class Student { String name; int age; Student() { name = "Unknown"; age = 0; } Student(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) { Student student1 = new Student(); Student student2 = new Student("John", 22); } } Here, we have two constructors with different parameter lists.
Constructor vs Method
| Constructor | Method |
|---|---|
| Used to initialize objects | Used to perform operations |
| Name must match the class name | Can have any valid name |
| No return type | Can have a return type |
| Called automatically during object creation | Called explicitly |
| Can be overloaded | Can be overloaded |
Important Points
- Constructors are used to initialize objects.
- Constructors are automatically called during object creation.
- Java provides a default constructor if we don't declare any constructor.
- Constructors can be overloaded.
- The
thiskeyword refers to the current object. - Constructors cannot be inherited or overridden.