exampracticeonline.com
Constructor

Constructors in Java

A constructor is a special method that is used to initialize an object.

A constructor must have same name as Class name. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. It can be used to initialize the objects to desired values or default values at the time of object creation. It is not mandatory for the coder to write a constructor for a class.

When is a constructor called

Java constructors are called when their objects are created.

When object is created using new() keyword, default constructor is called by default. Every class has a default constructor when we don't explicitly declare a constructor for any java class the compiler creates a default constructor for that class which does not have any return type.

Rules for creating a Java Constructor

  • A Constructor name must be the same as its class name.
  • Constructors do not have a explicit return type; not even void.
  • A Java Constructor cannot be abstract, static, final, and synchronized.
  • We don't need to create default constuctor compliler automatically creates.
  • We have to create default constructor if we created parametrized constructor in Java Class.

Types of Java constructors

There are two types of constructors:

  • Default constructor (no-arg constructor)
  • Parameterized constructor

Java Default Constructor

A constructor that has no parameter is known as default constructor.

If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class. And if we write a constructor with arguments or no-argument then compiler does not create default constructor.

The default constructor initializes any uninitialized instance variables by their below default values.

Type Default Value
boolean false
byte 0
short 0
int 0
long 0L
char \u0000
float 0.0f
double 0.0d
object Reference null

 


Syntax of default constructor:

<class_name>() {

}


Example of default constructor:

//Java Program of default constructor  

class Employee{  

         //creating a default constructor  

         Employee() { //default constructor

                  System.out.println("Employee default constuctor called");

         }  

         public static void main(String args[]) {  

                   Employee obj=new Employee();   // calling a default constructor  

         }  

}  


Output:

Employee default constuctor called

Java Parameterized Constructor

A constructor that has parameters is known as parameterized constructor.

If we want to initialize fields of the class with your own values, then use parameterized constructor.


Syntax of parameterized constructor

<class_name>(datatype var) {

}


Example of parameterized constructor

//Java Program of parameterized constructor  

class Employee{  

         int employeeId;

         String employeeName;

         //creating a default constructor  

         Employee() {

                  System.out.println("Employee default constuctor called");

         }  

        //creating a parameterized constructor  

         Employee(int employeeId, String employeeName) {

                  System.out.println("Employee parameterized constuctor called");

                  this.employeeId=employeeId;

                  this.employeeName=employeeName;

         }  

         public static void main(String args[]) {  

                   Employee obj=new Employee();   // calling a default constructor  

                   Employee obj=new Employee(1, "Danim");   // calling a parameterized constructor

         }  

}  


Output:

Employee default constuctor called

Employee parameterized constuctor called

Constructor Chaining

Constructor Chaining is used in concept of Inheritance.Consider a scenario where a parent class is extended by a child. Whenever an object of the child class is created, the constructor of the parent class is invoked first. This is called Constructor chaining.

Constructor Overloading in Java

  Aconstructor is just like a method but don't have return type. It can also be overloaded like Java methods.

Similar like method overloading, you can also overload constructors if two or more constructors are different in parameters.


Example of constructor overloading

public class Employee{  

         int employeeId;

         String employeeName;

         //creating a default constructor  

         Employee() {

                  this.employeeId=1;

                  this.employeeName="Vishnu";

         }  

        //creating a parameterized constructor  

         Employee(int employeeId, String employeeName) {

                  this.employeeId=employeeId;

                  this.employeeName=employeeName;

         }  

         private void showDetails() {

                  System.out.println("Employee id :"+this.employeeId);

                  System.out.println("Employee name :"+this.employeeName);

         }

         public static void main(String args[]) {  

                   Employee obj=new Employee();   // calling a default constructor  

                   Employee obj=new Employee(1, "Danim");   // calling a parameterized constructor

                   obj1.showDetails();
                   obj2.showDetails();

         }  

}  


Output:

Employee id :1
Employee name :Vishnu
Employee id :2
Employee name :Danim

Why do we Overload constructors ?

Constuctor overloading is done to construct object in different ways for example- 

  1. Create object by using default constructor (with default values)
  2. Create object by using parametrized constructor (with user input)

Does constructors return any value?

Yes, constructors return current instance of a class. But constructor don't have any return type in signature.

What's the difference between constructors and normal methods?

Constructors Methods
Constructors must have the same name as the class Methods can have different name also
A constructor is used to initialize the state of an object A method is used to expose the behavior of an object
Constructor can not return a value. Method can return a value or can be void.
The constructor is invoked implicitly The method is invoked explicitly
The Java compiler provides a default constructor The method is not provided by the compiler in any case
Constructor can called only once for a single object. Method can called multiple time for a single object.