Private enum constructor

JavaEnums

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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

Solution 7 - Java

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)

Solution 8 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpushyaView Question on Stackoverflow
Solution 1 - JavaDhruv GairolaView Answer on Stackoverflow
Solution 2 - JavaBeezView Answer on Stackoverflow
Solution 3 - JavaJérôme VerstryngeView Answer on Stackoverflow
Solution 4 - JavaOscar GomezView Answer on Stackoverflow
Solution 5 - JavaJeremyView Answer on Stackoverflow
Solution 6 - JavabeefyhaloView Answer on Stackoverflow
Solution 7 - Javauser104309View Answer on Stackoverflow
Solution 8 - JavaHarshaView Answer on Stackoverflow