DEV Community

Preethi Nandhagopal
Preethi Nandhagopal

Posted on

Constructor

What is constructor?
A special method that is automatically called when an object of a class created. Its main job is to initialize the object's data (variables) when the object is created.
Keypoints:
Same name as the class name.
No return type.
Automatically called runs as soon as you use new to create an object.
Default constructor is invisible.

This keyword:
In java, this keyword is a special reference variable that refers to the current object - the object whose method or constructor is being executed.
Used when local variable (method parameters) have the same name as instance variable.
syntax:
this.name=name;

Top comments (0)