What is constructor?
A Java constructor is special method that is called when an object is instantiated. In other words, when you use the new keyword. The purpose of a Java constructor is to initializes the newly created object before it is used.At the time of calling constructor, memory for the object is allocated in the memory.(we use new keyword to create an instance of a class, the constructor is invoked and the object of the class is returned. Since constructor can only return the object to class, it’s implicitly done by java runtime and we are not supposed to add a return type to it. If we add a return type to a constructor, then it will become a method of the class. This is the way java runtime distinguish between a normal method and a constructor.)
Syntax:
modifier class className {
modifier className(inputParameters){
//set fields and run methods needed on instantiation here
}
}
Rules for creating Java constructor:
There are two rules defined for the constructor.
- Constructor name must be the same as its class name
- A Constructor must have no explicit return type
- A Java constructor cannot be abstract, static, final, and synchronized[TBD]
Types of Constructor in Java:
There are three types of constructor in java.
- Default Constructor
- No-Args constructor
- Parameterized constructor
Default Constructor:
If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor.
No-Arg Constructors:
Similar to methods, a Java constructor may or may not have any parameters (arguments).
If a constructor does not accept(not give) any parameters, it is known as a no-argument constructor.
Parameterized Constructor:
A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructor with parameters).
Difference between constructor and method in Java
| Java Constructor | Java Method |
|---|---|
| A constructor is used to initialize the state of an object. | A method is used to expose the behavior of an object. |
| A constructor must not have a return type. | A method must have a return type. |
| The constructor is invoked implicitly. | The method is invoked explicitly. |
| The Java compiler provides a default constructor if you don’t have any constructor in a class. | The method is not provided by the compiler in any case. |
| The constructor name must be same as the class name. | The method name may or may not be same as the class name. |
Constructor overloading in Java:
We can overload constructors like methods. The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
Sometimes, we need to use multiple constructors to initialize the different values of the class.
We must also notice that the java compiler invokes a default constructor when we do not use any constructor in the class. However, the default constructor is not invoked if we have used any constructor in the class, whether it is default or parameterized. In this case, the java compiler throws an exception saying the constructor is undefined.
Use of this () in constructor overloading:
We can use this keyword inside the constructor, which can be used to invoke the other constructor of the same class.
Reference:
https://www.javatpoint.com/constructor-overloading-in-java
https://www.digitalocean.com/community/tutorials/constructor-in-java
Leave a comment