A constructor is one of the members of a class, which is used is to initialize the instance variables of class at the time of Object Creation.
Important Points About Constructor in Java
- The constructor name must be the same as the class name.
- Constructors cannot have any return type, not even the void.
- The whole purpose of constructors is to assign some value to the data member or instance variables at the time of Object Creation.
You can see in above image the name John is assigned into studentName variable at the time of Student Object creation.
Types of Constructor in Java
- Default Constructor
- Parameterized Constructor
Default Constructor in Java
When the developer or users does not create any constructor, then the compiler automatically creates a constructor that constructor is called Default Constructor.
As you can see in the above image the compiler created the default constructor.
Note: You can also create your own default constructor the same as on the right side of the above image, if you write same line of code in left side also, then in that case compiler will not create a constructor.
Important points About Default Constructor
- It does not accept any parameter while calling this constructor you don’t have to pass any value as an argument.
- The access modifier of the default constructor will be the same as the class in which it present. That means if the class is a public access modifier, then the constructor will also have the same public access modifier.
- It does not have any return type not even void.
Parameterized Constructor in Java
A constructor that accepts the parameter, when it gets invoked or called, In other words, The constructor in which we pass an argument while calling it is known as Parameterized Constructor.
Important points About Parameterized Constructor
- It accepts any parameter while calling this constructor, you can pass any value as an argument.
- Any access modifier such as private, public, protected, a default can be declared to the parameterized constructor.
- It does not have any return type not even void.
Constructor Overloading in Java
The process of having more than one constructor in a class with the same name but the change in a signature, then it is called Constructor Overloading.
Change in Signature refers to any for the following points.
- Change in the number of parameters constructor can accept at the time of invocation.
- Change in the sequence of parameters passed.
- Change in a datatype of the parameters while calling the constructor.
Important points of Constructor Overloading
- The constructor name does not use for calling any overloaded constructor.
- A constructor can call only one constructor, not more than one.
- Inside the constructor, calling the other constructor must be the first executable statement.
Constructor Chaining in Java
A constructor of a class can call the other constructors of the same class or the constructor of the immediate superclass, then this calling concept is called Constructor Chaining.
Rules of Constructor Chaining
- One constructor can call only one constructor, which be either from the same class or from the superclass.
- Calling the constructor must be the first executable statement.
Note: Every Constructor (Default or Parameterized) automatically calls its immediate superclass zero parameter constructor.
How to Perform Constructor changing in Java?
There are two ways to achieve constructor changing in Java.
- this();
- super();
this()
It is called this calling statement, which is used to call the overloaded constructor, which means the constructor of the class can call the constructor of the same class with the use of this() calling statement.
super()
It is called a super calling statement, which is used to call or invoke the constructor of the superclass, which means from subclass you can call the superclass constructor with the use of super() calling statement.
Difference between this and this() in Java
this | this() |
---|---|
this is a keyword. | It is not a Keyword. |
this represents the current invoking object which is used to call the instance method or variables | It is used to call an overloaded constructor. And one this() will only call one constructor, not more than one. |
Difference between constructor and method
- The name of the constructor should always be the same as for Class, but in case of a method, the name can be anything.
- The constructor does not have any return type not even the void but the method can have any return type such as int, float, Object, String, void, or any other datatype.
Can be the method name is the same as the Class name?
Yes, but it is not a good practice.
Thanks a lot for reading my article. It’s highly appreciated. I hope you must learn something new from here. If you any queries or doubt please let me know, I love to clear your all doubts as soon as possible.
And if you any suggestions or find any mistake in concepts, Please let me know. If you want to learn some other concept of Java, please check out the articles list.