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:

[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.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net