Re: Problem with Eclipse and Enum's

S

Steven

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

I was trying to help out with this question in comp.lang.java.help but
am more confused by the implementation of Enums than I was when I
started. I thought I would cross post this to the programmer group to
get more eyes.

Can anyone give a brief overview of Enums in Java 5? They appear to be
more complex than their C++ friends.

--Steve
 
P

Peter MacMillan

Steven said:
I was trying to help out with this question in comp.lang.java.help but
am more confused by the implementation of Enums than I was when I
started. I thought I would cross post this to the programmer group to
get more eyes.

Can anyone give a brief overview of Enums in Java 5? They appear to be
more complex than their C++ friends.

--Steve

A java enum is a typesafe set of values. It's a singleton class that has
a special way of creating predefined instances.

Yes it does. 3.1 reports public enum constructors as an error. And they
are (otherwise you'd destroy the singleton property - ie. you arent
supposed to be able to just create new types of the enum - they are
predefined in the enum body).

While each enum member has no intrisnic value (Days.SUNDAY is
Days.SUNDAY - nothing else), you can assign values to them.

// -- example:

public class EnumDemo {

public enum Colour {
RED (0x00FF0000),
GREEN (0x0000FF00),
BLUE (0x000000FF);

private int argb;

private Colour( int argb ) {
this.argb = argb;
}

public int getARGB() {
return argb;
}

}

public static void main(String... args) {

for (Colour colour : Colour.values()) {
System.out.printf("%1$s\t0x%2$08x\n",
colour.name(),colour.getARGB());
}

}

}

//--

expected output:

RED 0x00ff0000
GREEN 0x0000ff00
BLUE 0x000000ff



Notice how the enum is essentially a class with no way to create a new
instance (the only instances in this case are RED, GREEN and BLUE). To
be able to create a new instance, say YELLOW, you would have to define
it in the enum. It wouldn't make sense to have a way of doing: Colour
YELLOW = new Colour(0x00FFFF00); because this "YELLOW" wouldn't be a
part of the enum. You'd want to add it to the enum.


The old java way of doing it (public final static int VALUE = 0;) lacked
the ability to enumerate over the defined values (see the
Colour.values() in the above example).

Enums should be treated as a fixed set of constants - nothing else. It
also means they are safe for serialization because it stores the value
as the type (eg. Colour.RED is stored as Colour.RED and not, say 1). You
can add new types and not worry about reading back serialized data
(since it stored Colour.RED and not 1).

I recommend looking over the api docs if you haven't already:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
 
S

Steven

To start off, Eclipse didn't seem to care that the constructors were
Yes it does. 3.1 reports public enum constructors as an error. And they
are (otherwise you'd destroy the singleton property - ie. you arent
supposed to be able to just create new types of the enum - they are
predefined in the enum body).

Thanks for responding, I knew posting to the programmer group would
get a bite :)

In my own defense let me just say that my version of Eclipse does
allow the constructors to Enums to be public. I am using build:
Version: 3.1.0
Build id: 200412162000

I am not sure off hand which milestone this equates to but I imagine
it is older than 3.1M6

--Steve
 
S

Steven

Thanks for responding, I knew posting to the programmer group would
get a bite :)

In my own defense let me just say that my version of Eclipse does
allow the constructors to Enums to be public. I am using build:
Version: 3.1.0
Build id: 200412162000

I am not sure off hand which milestone this equates to but I imagine
it is older than 3.1M6

--Steve

Ok ... I have installed M6.
My public constuctors are now illegal.

All is right with the world.
--Steve
 
M

MrFredBloggs

I am the OP from the other group regarding this matter.

Below is what I really meant to do. Fankx for your help, I understand
enums much better now.

public enum Day
{
SUNDAY(1),
MONDAY(2),
TUESDAY(3),
WEDNESDAY(4),
THURSDAY(5),
FRIDAY(6),
SATURDAY(7);

private int number;

private Day(int intIn)
{
number = intIn;
}

public int getDayNumber()
{
return number;
}
}

public class RunDay
{
public static void main(String[] args)
{
for (Day day : Day.values())
System.out.println(day.name() + ": " + day.getDayNumber());
}
}

OUTPUT:

SUNDAY: 1
MONDAY: 2
TUESDAY: 3
WEDNESDAY: 4
THURSDAY: 5
FRIDAY: 6
SATURDAY: 7
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top