Results 1 to 5 of 5

Thread: Space in enum

  1. #1
    Join Date
    Sep 2008
    Posts
    21

    Default Space in enum

    hi all:
    I tied this

    enum type --class ~.reference.IndianStates
    enum constant --name Andhra Pradesh

    but could see that IndianStates.java had the following entries

    public enum IndianStates {

    Andhra

    }

    anything I am missing/ don't know ?

    I was hoping it would be

    public enum IndianStates {

    Andhra Pradesh

    }

    thanks
    rp

  2. #2
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb Spaces aren't allowed within enum constants in Java

    Enum constants can't have spaces in them; your expected code wouldn't compile, regardless of whether you're using Roo. This would be OK:

    Code:
    public enum IndianStates {
    
        Andhra_Pradesh  // Note the underscore
    
    }
    Although personally I like to use all upper case for enum constants.
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  3. #3
    Join Date
    Sep 2008
    Posts
    21

    Default

    Thanks for your reply and pointing out the default behavior of enum.

    I was very keen to know that in Roo - how would anyone go about creating "drop downs" on the default UI it generates.

    One quick way was creating an enum and mapping it back to a domain class like shown in one of the Roo samples

    enum type --class ~.reference.PetType
    enum constant --name Dog
    enum constant --name Cat
    enum constant --name Bird

    and the later . . .

    entity --class ~.domain.Owner --extends ~.domain.AbstractPerson --testAutomatically
    .
    .
    .
    field enum --fieldName specialty --type ~.reference.Specialty --notNull false


    so this a a nice way to quickly create a drop down of pet type.
    what if the pet type had a space ? any thoughts if I could have used something else instead of enum then ?

    thanks

    rohit

  4. #4
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb Override toString()

    The drop-downs just show the toString() of the relevant object, so you can achieve the desired display values by overriding that method per enum and/or per constant as follows:

    Code:
        enum State {
            
            Andhra_Pradesh,    // uses the enum-level toString
            
            Gujarat {
                @Override
                public String toString() {
                    return "A totally different name";
                }
            };
            
            @Override
            public String toString() {
                return name().replace("_", " ");
            }
        }
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  5. #5
    Join Date
    Sep 2008
    Posts
    21

    Default

    you are abs rite.
    Thanks for the "core" java help !

    rohit

Posting Permissions

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