Which of the following statements is true? (a)

[A]. Class [B]

B. constructor executes when an object is new

[C]. Constructor must have the same name as class

[D]. A class can define only one constructor

Resolution:

Option 1: When we write no constructors in a Java class, Java provides a constructor with no arguments by default, so this option is incorrect. But as soon as we write either constructor, the default no-argument constructor is no longer provided. Therefore, in most cases, we recommend that after you write a parameterized constructor, you define a parameterless constructor manually, not just for the class, but more for its subclasses. But that’s just a suggestion, not a requirement if you don’t have subclasses or use them.

Second option: When an object is new, the new keyword is followed by the corresponding constructor of the corresponding class. So that choice is correct.

// Create an object of class Student
// Call the no-argument constructor of the Student class
new Student();
Copy the code

Option 3: The constructor name really must be the same as the class name, so the first half of this option is correct. However, there is no restriction in Java that a common method name cannot be the same as a class name, except that the code specification does not recommend that a common method name be the same as a class name. So this option ends up being wrong.

Option 4: Not to mention that constructors support overloading, there are at least two types of parameterless constructors and parameterless constructors. So this option is wrong.