This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum Question

 
peter kosmas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic