Defining a private constructor

Figure 330. Define a private Day constructor Slide presentation Create comment in forum
public class Day {

  // Disallow object creation outside class
  private Day() {} 

  static public final Day
    MONDAY    = new Day(),
    TUESDAY   = new Day(),
  ...
    SUNDAY    = new Day();
}

Figure 331. Preventing undesired Day instance creation Slide presentation Create comment in forum
Class Screwed:

Day PAST_SUNDAY = new Day();

Lecture phpIntro = new Lecture(
     PAST_SUNDAY, "PHP introduction");

System.out.println(phpIntro.toString());

Compile time error:

'Day()' has private access in
'de.hdm_stuttgart.mi.sd1.
class_wrapper_private.Day'

Figure 332. Even better: Adding day name attribute Slide presentation Create comment in forum
public class Day {
  public final String name;

  private Day(final String name) {
    this.name = name;
  }
  static public final Day
    MONDAY    = new Day("Monday"),
  ...
    SUNDAY    = new Day("Sunday");

    public String toString() { return name; }
}