next up previous
Next: 1.4.3 Defining Functional Methods Up: 1.4 Java Classes and Previous: 1.4.1 Defining New Forms

1.4.2 Instantiating Class Definitions

Given the definition a concrete class C, it is instantiated by an expression of the form

new C( arguments )
where the list of arguments matches a constructor for the class C. A simple example of the use of new for the class Entry appears immediately above. In general, a class may have several different constructors, but each constructor must have a distinct list of argument types (so the Java compiler can determine which one is meant!).

The following expression creates a DeptDirectory containing the office and phone information for Corky and Matthias:

new Cons(new Entry("Corky","DH3104","x6042"), 
         new Cons(new Entry("Matthias","DH3106","x5732"), new Empty()))
This syntax is wordy but straightforward.



Robert Cartwright, Spring 1999