In the design recipe given earlier in this section, we suggested including sample instances for each concrete class in a data definition as static fields of the respective classes. We could add the following static fields in the classes Empty and Cons to perform this function.
... class Empty extends IntList { ... static only = new Empty(); } class Cons extends IntList { ... static oneElt = new Cons(1, Empty.only); static twoElts = new Cons(5, oneElt); static threeElts = new Cons(-10, twoElts); }
For large scale programs, the storage overhead involved in storing sample data values as static members may be significant. The static members take up space in memory whenever the program is run-regardless of whether the sample data values are used in that run. A good solution to this problem is for the Java programming environment to support the definition of sample test values for each class. Then the environment can include the test data values as static members of the class only when the code is being tested.