Problem with Eclipse and Enum's

Problem with Eclipse and Enum's

Post by MrFredBlog » Sat, 23 Apr 2005 08:55:56


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.
 
 
 

Problem with Eclipse and Enum's

Post by MrFredBlog » Sat, 23 Apr 2005 09:00:18

Hi All,

I think 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.

 
 
 

Problem with Eclipse and Enum's

Post by Steve » Sat, 23 Apr 2005 09:46:08


The following works but I don't know why your version doesn't work.

public enum Day {
SUNDAY(1),
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
public Day() {}
public Day(int i) {}
}

--Steve
 
 
 

Problem with Eclipse and Enum's

Post by Steve » Sat, 23 Apr 2005 10:03:15

On Fri, 22 Apr 2005 00:46:08 GMT, Steven < XXXX@XXXXX.COM >




Ok based on the examples in
http://www.yqcomputer.com/
say it is behaving correctly. It gives an example using a Coin enum as
follows:

public enum Coin {
penny(1), nickel(5), dime(10), quarter(25);

Coin(int value) { this.value = value; }

private final int value;

public int value() { return value; }
}

This example uses the Coin(int) constuctor for every entry. Your
version requires the Day() and Day(int) because you are using two
types of declarations. It may be that what you want is something along
the lines of the following:

public enum Day {
Sun(1),
Mon(2),
Tue(3),
Wed(4),
Thr(5),
Fri(6),
Sat(7);

private final int value;
public int value() { return value; }
}

Having used a simpler form myself, this seems a little more than is
wanted.

--Steve
 
 
 

Problem with Eclipse and Enum's

Post by Steve » Sat, 23 Apr 2005 22:43:00


To start off, Eclipse didn't seem to care that the constructors were
public or private, not sure if that matters or not.

I don't know the answer to my following question, but I am taking a
guess based upon the example implementation.

Enums in Java seem as though they don't have a particular value. For
example Days.SUNDAY doesn't equal 1. I may be wrong and hope if
someone else has a better information they will post it. They appear
to be more complicated than the C++ cousines.

I am not sure if this implementation with empty constructor calls is
really what you have in mind.

Just thinking out loud.
--Steve
 
 
 

Problem with Eclipse and Enum's

Post by Peter MacM » Sun, 24 Apr 2005 03:43:15


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://www.yqcomputer.com/
and
http://www.yqcomputer.com/


--
Peter MacMillan
e-mail/msn: XXXX@XXXXX.COM