 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
MrFredBloggs@hotmail.com Guest
|
Posted: Thu Apr 21, 2005 11:55 pm Post subject: Problem with Eclipse and Enum's |
|
|
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.
|
|
| Back to top |
|
 |
Peter MacMillan Guest
|
Posted: Fri Apr 22, 2005 6:43 pm Post subject: Re: Problem with Eclipse and Enum's |
|
|
[email]MrFredBloggs (AT) hotmail (DOT) com[/email] wrote:
| Quote: | 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/communityprocess/jsr/tiger/enum.html
and
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
--
Peter MacMillan
e-mail/msn: [email]peter (AT) writeopen (DOT) com[/email]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|