Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Trouble with enums on practice tests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Trouble with enums on practice tests" Watch "Trouble with enums on practice tests" New topic
Author

Trouble with enums on practice tests

E McKenney
Greenhorn

Joined: Sep 01, 2006
Posts: 18

I keep missing questions on enums because I can't figure out when the enum value *must* be qualified with the enum class name and when it doesn't have to be. (I.e., for enum "MyEnum", when value FOO must be referred to as MyEnum.FOO vs. when it you could just use FOO.)

I know that in a switch statement, the case labels don't have to be qualified; that makes sense since you are using an enum variable for the switch test. But I thought that was the only time, outside the enum definition.

Then I did a practice test question where an enum was imported from another package, and the enum values were used without qualification, e.g. just calling them FOO. I thought that was incorrect syntax so I chose "compilation fails" for the answer - but that was wrong!

Is there a succinct rule for knowing when to qualify? At the moment I'm just concerned with what would be legal Java syntax; personally I would always want to qualify the values to make the code clearer.

Thanks!
b.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you use static import (in Java 5.0) from a package:

you do not need to prefix the enum constant with the enum's name.
Is that what caught you out?

Static imports are not only used for enums though.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
E McKenney recently wrote:
...I know that in a switch statement, the case labels don't have to be qualified;...

Wasn't it must not be qualified in a case label?



Yours,
Bu.


all events occur in real time
E McKenney
Greenhorn

Joined: Sep 01, 2006
Posts: 18

Originally posted by Burkhard Hassel:

Wasn't it must not be qualified in a case label?

Yours,
Bu.



Good question! That's just the sort of thing that keeps catching me. I read the study guides and learn how things should be done, but it's not always clear whether it's *legal* to do them differently. And then I get a question on a practice exam that breaks one of those rules, and I'm not sure whether that means it won't compile, or just that it would be frowned upon.

E.g. one question that caught me more than once in the Rules Roundup was the one about whether a subclass can define a method that's final in the superclass. I may not be quoting the exact wording correctly, but even if you know that (a) you can create a method with the same signature in the subclass and (b) it won't technically be overriding the method in the superclass, it's still hard to be sure what the particular question has in mind.

But, I digress... !! I just remembered, this thread is about enums.

(Although my question about enums *is* really just an instantiation of my general trouble with distinguishing "poor coding practice" from "illegal Java code".)

b.
[ September 09, 2006: Message edited by: E McKenney ]
E McKenney
Greenhorn

Joined: Sep 01, 2006
Posts: 18

Originally posted by Barry Gaunt:
If you use static import (in Java 5.0) from a package
you do not need to prefix the enum constant with the enum's name.
Is that what caught you out?

Static imports are not only used for enums though.



That could have been it. I'll watch for that in the future.
Is that the only case besides the case labels where it's true?

Thanks!
b.
E McKenney
Greenhorn

Joined: Sep 01, 2006
Posts: 18

Originally posted by Burkhard Hassel:
Wasn't it must not be qualified in a case label?


You're right. In fact you can get some interesting compile errors if you do qualify it. I wrote a quick program with a switch statement, and qualified some of the case labels but not others. The ones that were qualified got errors, as you predicted, but the compiler threw in a couple of extras:



I guess when the compiler complains about the qualified enum value, it must substitute something generic there before parsing the next statement. Then when it does the same thing on the next label, that results in a "duplicate case label". Obviously I'm not giving the compiler enough to do, if it's starting to create its own errors...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with enums on practice tests