/home/wpollock1/public_html/AJava/Student.java

// Student.java - A demo of using optional constructor arguments by
// using overloaded constructors.  Student objects represent students
// enrolled in a college course and have many attributes not shown.
// This is sometimes called the "telescoping constructor pattern".
// (Validity checking of arguments and other details omitted for clarity.)
// Note, this same technique can be used for methods with optional
   arguments as well.
//
// Written 2/2013 by Wayne Pollock, Tampa Florida USA.

class Student
{
   private static int nextStudentID;

   static {
      // Typically fetched from a DB at application (JVM) startup:
      // ...
      nextStudentID = ...;
   }

   // Add a shutdown hook to save the current value of nextStudentID
   // to the DB when the application (the JVM) exits:
   // ...

   private int studentID;
   private String lastName;
   private String firstName;
   private String address;
   private String homePhone;
   private int level;  // 1 = freshman, ...
   private Date enrolled;

   // The gender really should be an enum!  The version here, using
   // a char, masked an error for several years, with one of the
   // constructors taking an "int", intending that to be "level":

   private char gender;  // 'M' or 'F', 'U' = unknown
   // ...

  public Student ( String lastName )
  {  this( lastName, "" );  }

  public Student (String lastName, String firstName )
  {  this( lastName, firstName, "Unknown", "", 1, new Date(), 'U' );  }

  public Student (String lastName, String firstName, String address )
  {  this( lastName, firstName, address, "", 1, new Date(), 'U' );  }

  // This constructor takes an int, but a char "promotes" to an int, so
  // the code "new Student ("Last", First", 'M')" calls this constructor,
  // rather than cause a compiler error.  The correct fix is to use enums
  // for the gender field, but as enums haven't been covered yet, I will
  // fix the bug by adding another constructor that takes a char:

  public Student (String lastName, String firstName, int level )
  {  this( lastName, firstName, "Unknown", "", level, new Date(), 'U' );  }

  public Student (String lastName, String firstName, char gender )
  {  this( lastName, firstName, "Unknown", "", 1, new Date(), gender );  }

  //////////////////////////////////////////////////////////////////////
  // Repeat for all allowed combinations of optional arguments!  Note //
  // not all combinations are possible (e.g., last, first, phone).    //
  // (How many constructors will there be?)                           //
  //////////////////////////////////////////////////////////////////////

  // This is the main constructor (which does all the real work),
  // and which is ultimately invoked by all the other constructors:
  public Student (String lastName, String firstName, String address,
      String homePhone, int level, Date enrolled, char gender )
  {
     this.studentID = nextStudentID;  ++nextStudentID;
     this.lastName = lastName;
     this.firstName = firstName;
     this.address = address;
     this.homePhone = homePhone;
     this.level = level;
     this.enrolled = enrolled;
     this.gender = gender;
  }

   // public Student methods go here:
   // ...
}


// Sample use (good luck figuring out which combinations and the
// order of arguments to use):

class StudentTest
{
  public static void main ( String [] args ) {
    Student s1 = new Student( "Doe" );
    Student s2 = new Student( "Piffl", "Hymie", 'M' );
    // ...
  }
}