Close Menu
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Facebook X (Twitter) Instagram
Developers GroundDevelopers Ground
Facebook X (Twitter) Instagram
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Developers GroundDevelopers Ground
Home Java Constructors
Java

Java Constructors

Akshay KulkarniBy Akshay KulkarniJanuary 2, 2024Updated:April 6, 2025No Comments4 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Reddit WhatsApp Email
Share
Facebook Twitter LinkedIn Pinterest WhatsApp Email

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.
java
public 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.
java
public 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

  1. 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 usingthe newkeyword.
  2. Final Constructor:

    • The final keyword 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 as final would prevent any subclass from providing its own implementation of the constructor. This goes against the principles of inheritance and object-oriented programming.
  3. Abstract Constructor:

    • Constructors cannot be declared as abstract either. 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.

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.

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
Akshay Kulkarni

Related Posts

Understanding the Java String Class

April 10, 2025

Array in Java

February 19, 2024

Control Statements

January 16, 2024
Leave A Reply Cancel Reply

Featured Posts
  • 1
    Understanding the Java String Class
    • April 10, 2025
  • 2
    Array in Java
    • February 19, 2024
  • 3
    Control Statements
    • January 16, 2024
  • 4
    Java Objects
    • January 11, 2024
  • 5
    Java Constructors
    • January 2, 2024
Facebook X (Twitter) Instagram Pinterest
© 2025 Developers Ground, All rights reserved.

Type above and press Enter to search. Press Esc to cancel.