What is constructor in java ?

A constructor is a special method used to initialize objects in java.

We use constructors to initialize all variables in the class when an object is created. As and when an object is created it is initialized automatically with the help of constructor in java.

We have three types of constructors

  1. Default Constructor
  2. No-arg constructor
  3. Parameterized Constructor

Example:-

Output:-