हिंदी

Answer the following in brief. Give a program code example wherever applicable. Constructors cannot be called. Then when do they get executed?

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 4: Constructors - Exercises [पृष्ठ ११५]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 4 Constructors
Exercises | Q 3. (v) | पृष्ठ ११५
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×