KnowledgeBoat Logo
|

Computer Applications

How do we invoke a constructor ?

Java Constructors

3 Likes

Answer

A constructor is invoked automatically when an object of a class is created. The constructor is called using the new keyword followed by the name of the class and a set of parentheses. If the constructor requires arguments, the arguments are passed inside the parentheses.

For example, suppose we have a class named Person with a parameterized constructor that takes two arguments name and age. To create an object of this class and invoke the constructor, we would use the following code:

Person personObj = new Person("John", 30);

Answered By

2 Likes


Related Questions