go to previous page   go to home page   go to next page

Answer:

Yes. To construct an object, there must be a constructor.


Default Constructor

Here is where the constructor is used in the main() method:

HelloObject anObject = new HelloObject();     // 2.  a HelloObject  is created using its 
                                              //     default constructor

Here is the class definition of HelloObject:

class HelloObject                                  
{                                                  
  void speak()                                     
  { 
    System.out.println("Hello from an object!");
  }
}

There is no constructor in the class definition. This is OK. A class will have a constructor even if one is not explicitly included in the class definition. If the class definition does not include a constructor a default constructor is automatically supplied by the Java compiler.


QUESTION 12:

So, it looks like the default constructor does no work. Is this correct?