Agile Javaв„ў: Crafting Code with Test-Driven Development
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: [1] The Java compiler does allow you to define multiple constructors in a single class; this will be discussed later. 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. |