Introduction to Java Constructors
- Java constructors initialize the state of an object and serve as special members of the class.
- Since constructor is a special member of the class, its name and class name should be the same.
- Constructor does not have any return type
- The compiler or the user must declare a constructor for every Java class.
- If the compiler declares the constructor, we refer to it as a default constructor.
- The compiler will define the constructor only if a user-defined constructor is absent and it can define only zero parameterized constructor.
- If the programmer defines the constructor then we refer to it as a user-defined constructor.
- Users can define zero parameterized as well as parameterized constructors.
- Execution of the constructor takes place at the time of object creation.
javapublic class Book{ //instance variables int bookId; String bookName; double bookPrice; //non-parameterized constructor Book(){ System.out.println("This is non-parameterized constructor"); } //parameterized coonstructor Book(int bookId, String bookName, double bookPrice){ this.bookId=bookId; this.bookName=bookName; this.bookPrice=bookPrice; } void display(){ System.out.println("Book Id: "+bookId); System.out.println("Book Name: "+bookName); System.out.println("Book Price: "+bookPrice); } } public class MainApp{ public static void main(String[] args){ Book b1=new Book(); Book b2=new Book(1, "JAVA", 2500.00); b2.display(); Book b3=new Book(2, "SPRING", 3200.25); } }
Output:
This is non-parameterised constructor
----------------------
Book Id: 1
Book Name: JAVA
Book Price: 2000.00
----------------------
Book Id: 2
Book Name: SPRING
Book Price: 3200.25
What is the use of ‘this’ keyword?
- ‘this’ is the special keyword that always points to the current class object.
- It is mainly used to differentiate between local variables and instance variables if they have the same identifier.
- We cannot use ‘this’ keyword in a static context but use it in non-static methods or constructors.
Java Constructor Overloading
- Constructor overloading is the process of creating multiple constructors of the same class with different parameters.
- At the time of constructor overloading parameters must be different either by length or datatype.
- By using constructor overloading we can create different objects of the same class.
javapublic class Employee{ int empId; String empName; double empSalary; int yearsOfExperience; Employee(int empId, String empName, double empSalary, int yearsOfExperience){ this.empId=empId; this.empName=empName; this.empSalary=empSalary; this.yearsOfExperience=yearsOfExperience; } Employee(int empId, String empName, double empSalary){ this.empId=empId; this.empName=empName; this.empSalary=empSalary; } } class MainApp{ public static void main(String[] args){ //For employee with experience Employee e1=new Employee(1, "JOHN", 20000, 2); e1.display(); //For employee with no experience Employee e2=new Employee(1, "SMITH", 12000); e2.display(); } }
Output:
1 JOHN 20000.00 2
2 SMITH 12000.00 0
In the above example, we declare two constructors for the Employee class. The constructors differ in the number of parameters. The first constructor will be called if the employee has any experience, while the second constructor can be called otherwise.
Constructors cannot be static, final or abstract due to the following reasons
Static Constructor:
- Since static members and methods belong to the class itself and not to instances of the class, having a static constructor doesn’t make logical sense because in Java, constructors are automatically called when an instance of a class is created using
the newkeyword.
- Since static members and methods belong to the class itself and not to instances of the class, having a static constructor doesn’t make logical sense because in Java, constructors are automatically called when an instance of a class is created using
Final Constructor:
- The
finalkeyword is used to indicate that a variable, method, or class cannot be further modified. Constructors are meant to initialize the state of an object, and marking a constructor asfinalwould prevent any subclass from providing its own implementation of the constructor. This goes against the principles of inheritance and object-oriented programming.
- The
Abstract Constructor:
- Constructors cannot be declared as
abstracteither. Abstract methods are meant to be implemented by concrete subclasses, but constructors are automatically called when an object is instantiated. Having an abstract constructor wouldn’t have a concrete implementation in any class as you cannot override it in subclass.
- Constructors cannot be declared as
In summary, constructors are a special type of method designed for initializing objects, and their characteristics are distinct from static, final, or abstract members/methods in Java.

