Default Constructor

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
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

But here is the class definition of HelloObject:

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

There is no constructor described 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?