A brief story of constructors in Java

Blog
Spread the love

A constructor is a special method that is automatically invoked when an instance of a class is created. They normally perform initialization of the object or setup operations such as storing the values in the instance fields. They help in the construction of an object, so they are called Constructors.

The process of putting the values into the properties of an object while creating it is known as object initialization, and the values are known as initial values.

Note- After creating an object, any change in the properties is called modification, not initialization

Process of implementation of the constructor:-

Its name should be the same as the name of the class.

It should not have a return data type.

It may have parameters and maybe not.

A constructor cannot be marked as final.

Constructors cannot be overridden.

Constructors cannot be made static.

A constructor can be made private, but it will be a singleton class.

conimplementation_java

(Code snippet of Employee class with Constructor)

In the above snippet, a class Employee has a property salary, which is initialized by the constructor Employee ().

As we cannot call Constructor explicitly, so it invokes implicitly while creating the object. We can see this in the below snippet while creating the object of the Employee class constructor will automatically be invoked.

conimplementation_java

(Implicit invocation of Constructor)

Features of constructors:-

If you want to perform any task only once in the life cycle of an object, then you have to put the code of that task into the constructor.

If you want to provide common resources like Database connection, Network connection, creating a GUI (Graphical User Interface), etc. to each object of your class while creating it, then put the code of these resources into the constructor.

It always returns the value as the Reference ID (Address of memory location where the object is saved).

No class can exist without a constructor. (this should follow all OOPs-based language)

It reduces the complexity of the code.

You can also create a method at the class level to put the value of properties, but it is the modification, not initialization.

There may be more than one constructor in a class.

Types of Constructors:-

Default Constructor

Parameterized Constructor

Non-parameterize Constructor

Default Constructor:-

If we do not write a constructor in a class, JVM automatically
provides one constructor during the compilation of the class. The constructor that Java provides is known as the “default constructor.” The default constructor doesn’t have arguments. By default, it provides all of the object’s numeric fields to 0 and Boolean fields to false. If the object also has any fields such as reference variables, the default constructor sets them to the special value “null, “ meaning that they do not reference anything.

Java only provides a default constructor when you do not write your own constructor for a class.

conimplementation_java

(Default constructor)

As you can see in the above snippet, Constructor is not created. So while the compilation compiler will inject a constructor by default, that constructor is known as Default Constructor. (No class can be possible without a constructor)

Parameterized Constructor:-

When we create a constructor in such a way that it accepts the arguments during the object creation, such type of constructor is known as parameterized constructor.

Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as arguments/parameters.

conimplementation_java

(Parameterized constructor)

In the above snippet, we have created a constructor which takes the parameter/argument. While invoking that constructor in the main method we have to pass the value as arguments. Since in this code we have already provided a constructor so the compiler will not inject the default constructor.

Parameterized constructor provides a different way to instantiate objects with distinct values.

Non-parameterize Constructor:-

When you created a constructor in the class, but no value passed as the argument/parameter. It seems like a default constructor but some small differences are present.

The compiler can inject a default constructor when you forget to provide a constructor on your side. So we cannot create a Default constructor, but we can create a non-parameterized constructor which looks like a Default constructor.

conimplementation_java

(Non-parameterized constructor)

In the above snippet, you can find a constructor but nothing is passed as an argument and still, it initializes the object. This constructor was inserted by you, not by the compiler.

Constructor Chaining:-

Constructor chaining occurs when a class inherits another class. Due to inheritance, a child class inherits the properties of the parent class. Both the parent and child classes may have constructor methods. When we create an object of a child class, we call it a constructor. It initializes child class attributes; now, we need to invoke the parent constructor. To achieve this, Java provides a super keyword through which we can pass arguments to the superclass constructor. For more clarity, see the constructor chaining example below

conimplementation_java

(Constructor Chaining)

It is a process of calling one constructor from another constructor concerning another object.

If you want to provide multiple resources (Database connection, Network connection, GUI, etc.) to each object of your class while creating it, rather than putting the code of each resource in a single constructor, make a separate constructor of each resource and then create a chain. It is used to create multiple resources for flexibility.

Like methods, constructors can also be overloaded. Since all the constructors in a class have the same name as the class, their signatures are differentiated by their parameter lists.

It is possible to use this() constructor, to implement local chaining of constructors in a class. The this() call in a constructor invokes the other constructor with the corresponding parameter list within the same class. Java requires that this() call occur as a constructor’s first statement.

Rules to achieve Constructor Chaining:-

Whenever you are achieving constructor chaining using a this() then it must be the first line in any constructor.

Whenever you achieve constructor chaining using a this(), there must be at least one constructor that does not have a this().

bluethinkinc_blog

bluethinkinc_blog

2023-02-10

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs