For Programmers: Free Programming Magazines  


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

Steven

2005-04-22, 3:58 am

On 21 Apr 2005 17:00:18 -0700, MrFredBloggs@hotmail.com wrote:

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


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
Steven

2005-04-22, 3:58 am

On Fri, 22 Apr 2005 00:46:08 GMT, Steven <steven.green30@verizon.net>
wrote:

>On 21 Apr 2005 17:00:18 -0700, MrFredBloggs@hotmail.com wrote:
>
>
>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


Ok based on the examples in
http://jcp.org/aboutJava/communityp...tiger/enum.html I would
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
MrFredBloggs@hotmail.com

2005-04-22, 3:58 am

I tried the following and it worked.

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

It uses private constructors though, according to Eclipse, public ones
aren't allowed (is that correct, must they always be private?).

Fankx for the help,

Fred.

Steven

2005-04-22, 4:01 pm

On 21 Apr 2005 18:15:28 -0700, MrFredBloggs@hotmail.com wrote:

>I tried the following and it worked.
>
>public enum Days
>{
> SUNDAY(1),
> MONDAY,
> TUESDAY,
> WEDNESDAY,
> THURSDAY,
> FRIDAY,
> SATURDAY;
> private Days() {}
> private Days(int i) {}
>}
>
>It uses private constructors though, according to Eclipse, public ones
>aren't allowed (is that correct, must they always be private?).
>
>Fankx for the help,
>
>Fred.

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com