Defining a private constructor
public class Day { // Disallow object creation outside class private Day() {} static public final Day MONDAY = new Day(), TUESDAY = new Day(), ... SUNDAY = new Day(); }
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' |
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; } }