Results 1 to 6 of 6
  1. #1
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default Problem with enums

    Recently , I am Learning Enums and their uses , but unfourtunately Eclipse don't recognize enums .

    I declare it this way :

    Java Code:
    Class class
    {
    enum level { low , medium , high }
    }
    Eclipse just don't recognize this code .

    Thanks for helping !

    Tony Stark

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem with enums

    Normally I would say try "class Class" instead. But Class is also a Java class
    which could cause some confusion. So use something like "class Clazz"

    The real issue was your capitalization.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Problem with enums

    If you wanna do it according to the "rules" of Java naming, then your code would look like this:

    Clazz: uppercase letter since it's a class definition
    enum name Level: uppercase letter since it's a class definition
    enum entries: ALL CAPS since they are effectively constants

    Java Code:
    class Clazz {
       enum Level { 
            LOW, 
            MEDIUM,
            HIGH
       }
    }
    Line breaks in are optional. I think it's better readable and easier to maintain like this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Problem with enums

    And why are you declaring the enum inside another class?

    You could simply (in a file called Level.java):
    Java Code:
    public enum Level{
        LOW, MEDIUM, HIGH
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Problem with enums

    Quote Originally Posted by Tolls View Post
    And why are you declaring the enum inside another class?

    You could simply (in a file called Level.java):
    Java Code:
    public enum Level{
        LOW, MEDIUM, HIGH
    }
    There are use cases for nested enums inside classes. It's more a logically grouping than anything else, but it does make sense.
    Java Code:
    class Paper
       enum Size {A4, LETTER}
       enum Orientation {PORTRAIT, LANDSCAPE}
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Problem with enums

    Yes, but not when you're just learning about enums in general.
    Besides, we usually just stick them in a suitable package...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Puzzled by enums
    By radiodave in forum New To Java
    Replies: 11
    Last Post: 05-13-2015, 12:37 AM
  2. Replies: 5
    Last Post: 10-31-2012, 05:34 PM
  3. Enums taking in enums?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 06-11-2011, 01:40 PM
  4. Returning flags from enums
    By willemien in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 07:37 AM
  5. why we are using enums in Java?
    By manish.anchan in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 04:41 PM

Posting Permissions

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