Step 1: Understanding object creation in Java.
In Java programming, objects are created from classes. When an object is created using the
new keyword, Java automatically initializes the object so that it can be used properly within the program.
During this process, a special method known as a
constructor is automatically called. The constructor prepares the object by initializing its variables and setting up the initial state of the object.
Step 2: Understanding constructors.
A
constructor is a special method in Java that has the same name as the class and does not have a return type. Its main purpose is to initialize objects when they are created.
For example:
class Student {
Student() {
System.out.println("Object Created");
}
}
When an object of the
Student class is created, the constructor
Student() is automatically executed.
Step 3: Evaluation of options.
(A) Instance method: Incorrect. Instance methods must be called explicitly using an object.
(B) Constructor: Correct. It is automatically invoked when an object is created.
(C) Class method: Incorrect. Class methods must be called explicitly and are not automatically executed during object creation.
(D) Object method: Incorrect. This is not a standard term used for automatic invocation in Java.
Step 4: Conclusion.
Therefore, the method that is automatically invoked when an object is created in Java is the
constructor.
Final Answer: Constructor