• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors for Enum types....

 
Mallesham Karnati
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we need (i.e. generally advised) to use 'private' constructors for 'enum' types in Java (1.5)? .... Then, why do we have the option to use public constructors?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a guess.

Enums in 1.5 are just a special type of class. Because there is no way to limit the visibility of a constructor in any other class it was to difficult, without enough benifit, to force the constructors to be private.

Like I said, just a guess.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mallesham Karnati:
Why do we need (i.e. generally advised) to use 'private' constructors for 'enum' types in Java (1.5)? .... Then, why do we have the option to use public constructors?


Sun's API at :-

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Enum.html

states

Enum
protected Enum(String name,
int ordinal)

Sole constructor. Programmers cannot invoke this constructor. It is for use by code emitted by the compiler in response to enum type declarations. ".

Java Enum tutorials are located at :-

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enum constructor is private so that we do not instantiate may be.
But check it out even if you do not keep it private it should work.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santana Iyer:
Enum constructor is private so that we do not instantiate may be.
But check it out even if you do not keep it private it should work.


Sun Java API shows the enum constructor as protected.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Enum.html#constructor_summary
We cannot instantiate enum class directly and constructor description states that "Programmers cannot invoke this constructor".

Your second sentence "But check it out even if you do not keep it private it should work" is not clear. Are you talking of extending the enum class ?
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh,

The constructor which Santana was saying is the one that is declared inside the enum block, not the protected constructor of Enum class defined in the API. The constructor for an enum type is implicitly private; that is, with or without the modifier "private" being specified, the constructor is always private.

For example:


If a modifier other than "private" is placed before Day constructor, a compiler error will be generated.



Joyce
 
Mallesham Karnati
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------------------
public enum Day {
... public Day() { } // illegal
protected Day() { } // illegal
private Day() { } // ok
Day() { } // ok - implicit private ...}
------------------------------------------------------

Joyce,
In the above code, is Public Day(){} Illegal or not usually recommended?
I am just curious to know.
Regards,
Mallesham
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mallesham,

public Day() is illegal. The enum tutorial does mention about this: http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

Joyce
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic