218k views
4 votes
What is a Java constructor?

User Kolbasov
by
5.0k points

2 Answers

5 votes

A Java constructor is special method that is called when an object is instantiated. In other words, when you use the new keyword. The constructor initializes the newly created object.

User Stefan Bollmann
by
5.5k points
5 votes

Answer:

Java constructor is a special method defined in a class whose name s the same as the class name

Step-by-step explanation:

Java constructor is a special method defined in a class whose name is the same as the class name.

The constructor is called when the object is instantiated to initialize the fields in the object. Constructor can have no arguments or it can carry one or more arguments as per the class design. A default no argument constructor is made available for a class automatically even if one is not explicitly defined.

For example: MyClass c = new MyClass();

This will call the zero argument constructor of MyClass if it is defined or otherwise the default constructor for MyClass.

User TomWolk
by
5.8k points