In this Tutorial we learn about constructor in java, default and Parameterized Constructor in java, Rules and Purpose of a Constructor, Constructor Calling, Difference Between Constructor and Method.
What is constructor in java?
It is one of the members of the class, just like method and variable. And its name must be the same as a class name, and it can not have any return type, not even void. Constructors are executed when we create an object.
Rules For The Constructor in Java?
There are three rules to define for the constructor.
- The constructor name and class name must be the Same.
- It hasn’t any explicit return type, not even void.
- Constructors can’t be abstract, final, static, and also synchronized.
Read Also: Top Ways to do Method Overriding In Java?
Types of constructor in java:
These various types of constructors.
- Default constructor (no-arg constructor).
- Parameterized constructor.
Default Constructor in Java ?
A default constructor is a select type of constructor. Whenever we create an object using new () keywords. the default constructor is automatically created by the compiler (Not by JVM).
Question:- how to call default constructor from parameterized constructor in java?
Default constructors are used to initialize the resources or instance variables to default values. They do not take any Parameters. And the access modifiers of the default constructor are the same as the class.
Read Also:- Java Program-How To Count The Number Of Bits?
Note:- Why is its name a constructor? The simple answer is because it constructs the values at the time of object creation. When the class does not have any constructor. then the compiler will create a default constructor.
Parameterized Constructor in java?
The programmer or user creates these types of constructors. That’s why we called custom/user-defined or parameterized constructors.
Parameterized Constructor Example:
The primary purpose of a parameterized constructor is to initialize an instance variable to dynamic values or user-defined values at the time of object creation.
Java Constructor Example.
When Global and Local variables have the same name.
Note:- These types of Constructors can be parameterized or non parameterized. Only one constructor can be live, parameterized. or default in the class but not Both together.
Use or Purpose of a Constructor?
In simple words, both constructors are used to initialize at the time of object creation. The significant difference between the default and the parameterized constructor. we don’t need to initialize the default constructor because the compiler automatically generates it. Still the parameterized constructor gives an option to make it custom values.
Ways to Call Constructor?
In Java, we have three different ways to call Constructor.
- With the help of the “new” keyword.
- Using this() keyword.
- And using Super() Keyword.
Difference Between Constructor and Method?
|1||They are used to initialize the states of the object||Methods used for exposing the behavior of the class objects.|
|2||A constructor doesn’t have any return type.||The method has a return type.|
|3||They are invoking implicitly.||They are invoking explicitly.|
|4||When we don’t have in the class, the compiler generates a default constructor.||The compiler does not provide the method.|
|5||Make sure the constructor name and class name are the same.||Sometimes Method names and the class name are the same or sometimes not.|
- Full Explained Aggregation, Composition, And Association In Java.
- Overloading in Java? Types, Basic Advantages with Example | 2021
- Constructor in Java, Rules, Types, and Ways to Create Constructor | 2021
- Method Overriding In Java? Simple Ways to Overload Methods
- Data Types in java – Primitive and Non-Primitive Data Types