Advertisements
Advertisements
प्रश्न
Answer the following in brief. Give a program code example wherever applicable.
Constructors cannot be called. Then when do they get executed?
कोड लेखन
Advertisements
उत्तर
A constructor is executed automatically when an object of the class is created using the new keyword. It is used to initialize the object. When the object s is created using new Student(), the constructor is automatically invoked.
class Student
{
Student()
{
System.out.println("Constructor executed");
}
public static void main(String[]args)
{
Student s = new Student();
}
}shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
