Constructor looks like a method but it is in fact not a method. It’s name is same as class name and it does not return any value. You must have seen this statement in almost all the programs I have shared above:

MyClass obj = new MyClass();

If you look at the right side of this statement, we are calling the default constructor of class myClass to create a new object (or instance).

We can also have parameters in the constructor, such constructors are known as par

ametrized constructors.

public class ConstructorExample {

int age;
String name;

//Default constructor

//Parameterized constructor
ConstructorExample(String n,int a){;
public static void main(String args[]){
ConstructorExample obj1 = new ConstructorExample();
ConstructorExample obj2 =
new ConstructorExample(“”, 56);
System.out.println(” “+obj1.age);
System.out.println(” “+obj2.age);

OUPUT: 30 56