• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Enum Question

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ...
This piece of code is from the source question Bank ! the Sun Microsystems scjp training they have with mock exams
i would like to know why c.equals("BLUE") does not work and Other.Colors.RED.equals(c) works fine
what's the catch .(The reference did not give me a good clue just one line saying Lines 18 and 20 are correct
here is the code


and here is what it displays
red green

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because you are comparing a String with an instance of your enum, which can never be the same!

Note that every element of an enum has it's own instance.

like this:


whenever you assign an enum to somewhere, you are invoking it's constructor, hence get a fresh instance.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:whenever you assign an enum to somewhere, you are invoking it's constructor, hence get a fresh instance.


Can you show a source for this statement? I'm not sure that I agree with it.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:
Can you show a source for this statement? I'm not sure that I agree with it.



The fact that you can pass arguments to an enum supports this statement. Check this code...


I have to correct myself though. A call to the same Enum element will always return the same instance. My bad
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if (c.equals("BLUE"))
System.out.print("blue ");


How can this be possible??? as far as i think "BLUE" will be a String object in the String pool whereas the c is an enum reference variable which will have RED,GREEN,BLUE and YELLOW enum constants. The two cannot be meaningful equivalents at all because enum constants are not a string.


then you can compare

c.getName().equals("BLUE");


Well one more method is convert your enum constant to a String and then compare. The original code that you had given. change c.equals("Blue"); to c.toString().equals("BLUE");
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:I have to correct myself though. A call to the same Enum element will always return the same instance. My bad

Thanks for the clarification. This is what I remember reading when learning about enums and is why == will work with enums.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic