• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructors for Enum types....

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic