Home > Archive > Java Help > April 2005 > Problem with Eclipse and Enum's
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with Eclipse and Enum's
|
|
| MrFredBloggs@hotmail.com 2005-04-22, 3:58 am |
| Hi All,
I think that the following code is correct:
public enum Days
{
SUNDAY (1),
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
However it doesn't compile under Eclipse 3.1M6.
What am I doing wrong?
Fankx,
Fred.
| |
| Peter MacMillan 2005-04-22, 8:58 pm |
| MrFredBloggs@hotmail.com wrote:
> Hi All,
>
> I think that the following code is correct:
>
> public enum Days
> {
> SUNDAY (1),
> MONDAY,
> TUESDAY,
> WEDNESDAY,
> THURSDAY,
> FRIDAY,
> SATURDAY;
> }
>
> However it doesn't compile under Eclipse 3.1M6.
>
> What am I doing wrong?
>
> Fankx,
>
> Fred.
>
Your code is wrong.
MONDAY is equivlant to MONDAY() - the default constructor.
Remember that, when you create a class, you get a default empty
constructor unless you specify one (or the superclass excludes an empty
constructor).
You don't really want SUNDAY to equal 1. You want SUNDAY to equal SUNDAY
and that's what enums are for.
You might want something like:
public enum Day
{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
and then somewhere in code:
for (Day day : Day.values()) {
System.out.println(day.name());
}
The order of values() is guaranteed to be the same as the order the
elements are defined in the enumeration.
see:
http://jcp.org/aboutJava/communityp...tiger/enum.html
and
http://java.sun.com/j2se/1.5.0/docs...uage/enums.html
--
Peter MacMillan
e-mail/msn: peter@writeopen.com
|
|
|
|
|