Constructors-Parameterized & Non-Parameterized, This Keyword & Constructor Overloading
|A Constructor is very similar to a method in its structure but differs greatly as a constructor is used to initialize the objects of the class. A constructor has several constraints:
- The name of the constructor must always be the same as the class name.
- A constructor cannot have a return type not even void as the implicit return type of a constructor is the class type where belongs. But a constructor can have access specifiers.
- There is a default constructor added automatically in case we do not provide any custom implementation of the constructors.
It is known as constructor because it initializes the object of the class, i.e. it constructs the object by supplying it with values. A constructor is not considered as a member of the class. The reason is that it is an initializer and thus cannot be inherited. It simply belongs to the class for which it is created.
Parameterized and non-parameterized constructors
On the basis of parameters of the constructor, it can be divided into two categories:
- Non-parameterized Constructor/ Default Constructor: The constructors that have an empty parameter are known as non-parameterized constructors. They are used to initialize the object with default values or certain specific constants depending upon the user. In case the user does not define any constructor for the object then Java automatically creates a default constructor to assign the various members to default values depending upon their type.
- Parameterized Constructor: A parameterized constructor is the one that has defined parameters and arguments(as specified by the programmer) are sent to the constructor during object creation to initialize the object.
General Form of a Constructor
- Non-Parameterized Constructor/ Default Constructor:
class Demo { // instance variable declaration Demo() { // initialization statement } } // Object creation statement for the Demo class Demo objectName = new Demo();
- Parameterized Constructor:
// An example of parameterized constructor class Demo { int a, b; Demo(int a, int b) { this.a = a; this.b = b; } //A paramterized constructor can have this form as well /* Demo(int a1, int a2) { a = a1; b = b2; } */ // Object creation statement for the Demo class Demo objectName = new Demo(5, 6); // any values as desired by the user
The this Keyword
In the initialization of the parameterized constructor, we see the use of a new keyword- this. The ‘this‘ keyword is used to refer to the current object, i.e. the current object that is invoked within a constructor or a method, i.e. this can be used to refer to the instance variables and methods of the current class object that is being referenced.
In addition to this, ‘this’ keyword can also be returned to pass on the current state of the object or can also be passed as an argument to a method or constructor. ‘this()‘ is used to call the current class constructor. It is mostly used for chaining the constructors, i.e. establishing a link between them by calling a constructor from another constructor of the same class.
// refering to current object. For initializing the instance variable // (differentiates between the insatnce variable and the parameter variables) this.variableName = variableName; //passing the current object refernce to another method or constructor using this new Thread(this); //using this for constructor chaining. this(value); //returning the current state of the object return this;
Constructor Overloading
Like method overloading in Java, constructors can also be overloaded. All the constructors share the same name as the class name. The only difference being the difference in their parameters in the definition statement. This allows the compiler to differentiate between the different constructors of the same class. Many of Java’s built-in classes have overloaded constructors to allow the user to initialize the object in several different ways. One such example is the Thread class which has about 8 constructors.
Program to show the creation of constructors(both default and parameterized), this keyword and Constructor Overloading
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Constructors/ConstructorDemo.java
Output:-
a = 0
b = 0
c = C
a = 4
b = 5
c = O
Learning is the whetstone for great minds.So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Share and subscribe.
Keep Coding!! Happy Coding!! 🙂
Everythings are explained in very good manner by which anyone who read the content ,it understands very quickly