aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum Question" Watch "Enum Question" New topic
Author

Enum Question

peter kosmas
Ranch Hand

Joined: Aug 26, 2008
Posts: 79
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


Time is relative so there is no way i can be late.
~Albert Einstein~
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jul 15, 2009
Posts: 537

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");

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum Question
 
Similar Threads
Enum constants in Switch
New user: Enum with switch case
Doubt in enums
Enum optional semicolon
comments welcome to clearify...