Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messageOn 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
Post Follow-up to this messageOn 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
Post Follow-up to this messageI 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.
Post Follow-up to this messageOn 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
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.