Generated Java Print Method Format and Calling Parameters

The print method is provided in two forms with the following signatures:

   public void print (PrintWriter out, String varName, int level)

and

   public void print (PrintStream out, String varName, int level)

The fundamental difference between them is the out argument. In the first method, out is a PrintWriter object, and in the second method, it is a PrintStream object. In both cases, out specifies a stream to which output should be written. For text output (such as this method performs), PrintWriter is preferred over PrintStream and will usually perform better. However, the PrintStream form is provided for backwards compatibility and for writing to standard output (i.e. System.out).

The varName argument is used to specify the top-level variable name of the item being printed. Normally, this would be set to the same name as the variable declared in your program that holds the object being printed. For example, if you declared a variable called personnelRecord to hold a PersonnelRecord object, the varName object would be set to "personnelRecord".

The level argument is used to specify the indentation level for printing nested types. The user would always want to set this to zero at the outer-level.

For example, the call to print the personnelRecord from the previous examples would be as follows:

   personnelRecord.print (System.out, "personnelRecord", 0);

The output would be formatted as follows:

   personnelRecord {
      name {
         givenName = 'John'
         initial = 'P'
         familyName = 'Smith'
      }
      number = 51
      title = 'Director'
      dateOfHire = '19710917'
      nameOfSpouse {
         givenName = 'Mary'
         initial = 'T'
         familyName = 'Smith'
      }
      children[0] {
         name {
            givenName = 'Ralph'
            initial = 'T'
            familyName = 'Smith'
         }
         dateOfBirth = '19571111'
      }
      children[1] {
         name {
            givenName = 'Susan'
            initial = 'B'
            familyName = 'Jones'
         }
         dateOfBirth = '19590717'
      }
   }