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(); }
|
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; } }