Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messageMrFredBloggs@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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.