Results 1 to 9 of 9

Thread: enum confusion

  1. #1
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default enum confusion

    I have a little class that begins like this:

    Java Code:
    public class MyClass{
        enum MyEnum {ONE, TWO, THREE, FOUR}
        MyEnum number = MyEnum.ONE
    What I'd like to know is how I would change this so I could just say:

    Java Code:
        MyEnum number = ONE;

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: enum confusion

    Java Code:
    public class MyClass{
        enum MyEnum {ONE, TWO, THREE, FOUR}
        private static final MyEnum ONE = MyEnum.ONE;
        MyEnum number = ONE;
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: enum confusion

    Hmmm... well that's OK for a small enum. What if it has dozens or hundreds of entries? I'd rather not add a variable for each one. Is there a way to import those enum variables into the namespace in one statement?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: enum confusion

    Quote Originally Posted by gbritton View Post
    Hmmm... well that's OK for a small enum. What if it has dozens or hundreds of entries? I'd rather not add a variable for each one. Is there a way to import those enum variables into the namespace in one statement?
    Suppose stuff works the way you wanted it to work; also suppose you have an unrelated variable 'int ONE;' refering to 'ONE' confuses the compiler out of its mind ...

    kind regards,

    Jos

    ps. agreed, it is silly to make an exception for the case clauses of a switch on an enum ...
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: enum confusion

    Indeed, it's not just an exception, it's the LAW!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: enum confusion

    Quote Originally Posted by Tolls View Post
    Indeed, it's not just an exception, it's the LAW!
    Bull; I am the law ...

    kindest regards,

    Silv^H^H^H^HJos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: enum confusion

    Quote Originally Posted by gbritton View Post
    Hmmm... well that's OK for a small enum. What if it has dozens or hundreds of entries? I'd rather not add a variable for each one. Is there a way to import those enum variables into the namespace in one statement?
    If there were, Darryl would have told you right from the beginning.

    Why do you want to do this anyway? Don't tell me it is to save typing. And don't tell me it is for easier to read code, because as Judge Dr... JosAH already illustrated, this quest you're on only leads to epic name clashes, confused compilers, confused programmers and in the end: tears.

    (over-dramatized for added effect, imagine the Jaws tune playing in the background while reading that)

    And btw: if you're putting hundreds of entries into an enum you're either doing something brilliant or something very stupid. Its not a problem you're ever going to have, I hope.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    gbritton is offline Member
    Join Date
    Jul 2013
    Posts
    11
    Rep Power
    0

    Default Re: enum confusion

    thanks all! great input! (Though I doubt the compiler would get confused. It already detects duplicate variable names just fine :-))

    fwiw I noticed that if I flip things around, I CAN reference the enum's without the class ref. e.g. Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects) about half-way down under Planet

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: enum confusion

    Quote Originally Posted by gbritton View Post
    fwiw I noticed that if I flip things around, I CAN reference the enum's without the class ref.


    That code is inside the enum declaration. That's not something you would normally want to do in production code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. public static enum vs enum class
    By Dipke in forum New To Java
    Replies: 3
    Last Post: 08-30-2011, 10:45 AM
  2. Enum confusion
    By castiel in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 06:58 AM
  3. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  4. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  5. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •