What is a Constructor?

1 answers

A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java compiler (Javac) creates a default constructor for that class.

A default constructor in java is shown in the example below:

Default Constructor

public Man()
  System.out.println("Man is created");  

Constructor which takes a parameter is shown in the sample below:


private String name;
    public Employee(String name)
      this.name = name;
shilpigupta22 shilpigupta22 [Author] 7 months ago votes 0