Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Default Constructors

You may have noted that neither of the test classes, StudentTest and Course-SessionTest, contains a constructor. Often you will not need to explicitly initialize anything, so the Java compiler does not require you to define a constructor. If you do not define any constructors in a class, [1] Java provides a default, no-argument constructor. For StudentTest, as an example, it is as if you had coded an empty constructor:

class StudentTest extends junit.framework.TestCase {
   StudentTest() {
   
   }
   ...
}

The use of default constructors also implies that Java views constructors as essential elements to a class. A constructor is required in order for Java to initialize a class, even if the constructor contains no additional initialization code. If you don't supply a constructor, the Java compiler puts it there for you.

  • + Share This
  • 🔖 Save To Your Account