Re: Problem with Eclipse and Enum's

Discussion in 'Java' started by Steven, Apr 22, 2005.

  1. Steven

    Steven Guest

    On Fri, 22 Apr 2005 13:43:00 GMT, Steven <>
    wrote:

    >On 21 Apr 2005 18:15:28 -0700, 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


    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
     
    Steven, Apr 22, 2005
    #1
    1. Advertising

  2. Steven wrote:
    >>
    >>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
    >


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

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


    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


    --
    Peter MacMillan
    e-mail/msn:
     
    Peter MacMillan, Apr 22, 2005
    #2
    1. Advertising

  3. Steven

    Steven Guest

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

    >
    >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
     
    Steven, Apr 22, 2005
    #3
  4. Steven

    Steven Guest

    On Fri, 22 Apr 2005 21:11:19 GMT, Steven <>
    wrote:

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

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


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

    All is right with the world.
    --Steve
     
    Steven, Apr 22, 2005
    #4
  5. Steven

    Guest

    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
     
    , Apr 23, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    581
  2. Jerminia
    Replies:
    3
    Views:
    653
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    494
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    447
    Dave Thompson
    Jun 10, 2004
  5. Brian
    Replies:
    4
    Views:
    2,740
    Brian
    Feb 27, 2010
Loading...

Share This Page