pushya
pushya

Reputation: 4418

Private enum constructor

The constructor for this enum is private. What does that mean?

public enum SLocale {

    EN_US(Locale.US, "www.abc.com", "www.edc.com", "www.vvv.com",
            "www.earn.com");

    List<String> domains;
    Locale loc;
    IMap map;

    private SLocale(Locale loc, String... domains) {
        this.domains = Arrays.asList(domains);
        this.loc = loc;
        this.siteMap = Factory.getMap(loc);
    }

    public List<String> getDomains() {
        return domains;
    }

    public Locale getLoc() {
        return loc;
    }

    public ISiteMap getMap() {
        return map;
    }
}

Upvotes: 67

Views: 57823

Answers (8)

Dhruv Gairola
Dhruv Gairola

Reputation: 9182

A private constructor only allows objects to be constructed from within the class definition. Being an enum, it is easy to get confused, so I usually find it easier to think of an enum as a class with some special features. So when you write:

SLocale.EN_US

Basically, the parameters

Locale.US, "www.abc.com", "www.edc.com", "www.vvv.com", "www.earn.com"

will be passed to the private constructor so that the enum can be instantiated. Enum constructors have to be private.

Upvotes: 67

user104309
user104309

Reputation: 690

public enum Day {

    SUNDAY(), MONDAY, TUESDAY(2), WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;

    int value;

    private Day(int value) {
        System.out.println("arg cons");
        this.value = value;
    }

    private Day() {
        System.out.println("no arg cons");
    }

    public static void main(String args[]) {

    }

}

Output: no arg cons no arg cons arg cons no arg cons no arg cons no arg cons no arg cons

Imagine enum as the following :

SUNDAY() is equivalent to static final Day SUNDAY = new Day();

MONDAY is also equivalent to static final Day MONDAY = new Day(); // without paranthesis it calls the no-arg constructor/default no-arg constrctor if no other constructor

TUESDAY(2) is equivalent to static final Day TUESDAY = new Day(2);

Since enum has to be considered a special type of class,it allows for "static" object creation. Since you can't do object creations outside of the enum class,all object creations happen at the class declaration level itself and hence "static" for object creation makes sense here.

In Enum,all the object creations(i.e, static final constant creations,to be precise) must take place from within that enum class itself (and hence private constructor) because the purpose of Enum is to have only a fixed set of meaningful constants with respect to your application and eliminate unmeaningful statements/instantiations like Day SOME_EIGHTH_DAY_THINKING_TO_BE_VALID = new Day(8)

Upvotes: 4

Harsha
Harsha

Reputation: 1

I would think of Enums as singleton and hence the constructors must be private, if they are not singleton then think what all will go wrong. when u declare a constructor then you are implementing final static behaviour of java. you can initilaise once only. this sort of impl came out of the properties file or cfg files which need to be loaded once at the start of the application. problem with nromal enums and constants is that you have to change java code, and needs recompilation. but if you are loading from file then we can change it and restart, changes will take effect. hope I shed more light on this.

Upvotes: 0

You need this constructor to be private, because enums define a finite set of values (for example EN_US, EN_UK, FR_FR, FR_BE). If the constructor was public people could potentially create more values (for example invalid/undeclared values such as XX_KK, etc). This would extend the set of initially declared values.

Upvotes: 10

Jeremy
Jeremy

Reputation: 22415

In the case of enums, it means the same thing as making it package private. The only way to instantiate an enum is by declaring them within your enum class. Enums cannot have public constructors.

Upvotes: 4

Oscar Gomez
Oscar Gomez

Reputation: 18488

The Enums are required to have exclusively private constructors, this is because the Enum should be the only one responsible for returning the predefined instances.

Upvotes: 5

Beez
Beez

Reputation: 2101

From: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

You cannot actually have a public enum constructor.

Upvotes: 50

beefyhalo
beefyhalo

Reputation: 1821

It means no code other than the enum "class" itself is able to explicitly construct an enum object

Upvotes: 3

Related Questions