Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

enums and modifiers

Thread view: 
richnjones@gmail.com - 14 Nov 2007 21:26 GMT
Hello all,

I was messing around with enums and am confused about the modifiers I
can apply. I am using Eclipse and am not sure if I am misunderstanding
the error.

Consider this enum

enum Planets {Mercury, Venus}

I can put private/protected/public in front of it. At the moment I
consider it to have package access. This makes perfect sense

I can also add static in front of it. Enum is a class that extends
Object so that kind of makes sense. However I cannot add final in
front of it. Now I dont believe you can extends enums (why would you
want to) so having final might seem a little pointless but i do not
see why you cannot have it for completeness. (You can add final to
variables in Iiterfaces which are implicitly final) Also and more
confusingly when I put final in front of it then Eclipse says it is an
invalid modifier and that I can only have private/protected/public/
abstract/static. When I put abstract in front I get a compile error.
Is Eclipse wrong? Can you put an abstract in front of enums in some
cases? Is there any reason you cannot put final in front? (Assuming
you can use abstract then i can see why final isnt allowed). What use
would an abstract have?

abstract enum Planet {Mercury, Venus}         //does not compile

Any help is greatly appreciated
Thanks
R
richnjones@gmail.com - 14 Nov 2007 21:29 GMT
ah so the Enum class is abstract. This I believe is what the compiler
uses when it come across the enum keyword. That would explain why I
cannot use final. Is the fact that the class is abstract confusing
Eclipse which tells me I can use abstract?
Lew - 14 Nov 2007 22:17 GMT
> ah so the Enum class is abstract. This I believe is what the compiler
> uses when it come across the enum keyword. That would explain why I
> cannot use final. Is the fact that the class is abstract confusing
> Eclipse which tells me I can use abstract?

Don't know what's up with Eclipse, but the JLS clearly states:
> Enum types (§8.9) must not be declared abstract;
> doing so will result in a compile-time error.

and
> An enum type is implicitly final
> unless it contains at least one enum constant that has a class body.
> In any case, it is a compile-time error to explicitly declare an enum type to be final.

Also,
> Nested enum types are implicitly static.
> It is permissable to explicitly declare a nested enum type to be static.

There are more restrictions, too.
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9>

It's not to do with the implementation of enums as extension of Enum, rather,
with the language's laws.

Signature

Lew

richnjones@gmail.com - 15 Nov 2007 20:42 GMT
Hi Lew and Joshua,

Thanks for the replies. It is much clearer now. I am studying for my
SCJP and there is nothing like messing around with code to make you
confused. Thanks again

Richard

> > ah so the Enum class is abstract. This I believe is what the compiler
> > uses when it come across the enum keyword. That would explain why I
[quoted text clipped - 22 lines]
> --
> Lew
Joshua Cranmer - 14 Nov 2007 22:14 GMT
> I can put private/protected/public in front of it. At the moment I
> consider it to have package access. This makes perfect sense

Only if it is internal to a class. Top-level source declarations can
only have a package-protected or public access level, same as classes,
interfaces, and annotations.

> I can also add static in front of it. Enum is a class that extends
> Object so that kind of makes sense.

Once again, only if internal to a class.

> However I cannot add final in
> front of it. Now I dont believe you can extends enums (why would you
> want to) so having final might seem a little pointless but i do not
> see why you cannot have it for completeness.  (You can add final to
> variables in Iiterfaces which are implicitly final)

enums != interfaces.
The short answer: enums have nuanced finality/abstractness qualifiers.
Specifically, an enum of the sort:
enum Foo {
   ONE {
       public boolean getVar() { return true; }
   }, TWO {
       public boolean getVar() { return false; }
   }

   public abstract boolean getVar();
}

is actually composed with three classes internally (Foo, Foo$ONE, and
Foo$TWO) of which the first one is abstract and the other two are not.
Since an enum can actually be abstract in some cases and final in
others, neither qualifier is valid. See the JLS for more information.

> Also and more
> confusingly when I put final in front of it then Eclipse says it is an
> invalid modifier and that I can only have private/protected/public/
> abstract/static. When I put abstract in front I get a compile error.

See above.

> Is Eclipse wrong? Can you put an abstract in front of enums in some
> cases? Is there any reason you cannot put final in front? (Assuming
> you can use abstract then i can see why final isnt allowed). What use
> would an abstract have?

No, Eclipse is not wrong; it follows the JLS precisely in this instance.

JLS 3 §8.9 Enums:
Enum types (§8.9) must not be declared abstract; doing so will result in
a compile-time error. It is a compile-time error for an enum type E to
have an abstract method m as a member unless E has one or more enum
constants, and all of E's enum constants have class bodies that provide
concrete implementations of m. It is a compile-time error for the class
body of an enum constant to declare an abstract method.

An enum type is implicitly final unless it contains at least one enum
constant that has a class body. In any case, it is a compile-time error
to explicitly declare an enum type to be final.

Nested enum types are implicitly static. It is permissable to explicitly
declare a nested enum type to be static.

Those are the full rules for modifiers attached to enums.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Roedy Green - 15 Nov 2007 23:02 GMT
>I was messing around with enums and am confused about the modifiers I
>can apply

See http://mindprod.com/jgloss/enum.html

If you look at how enums get turned into ordinary classes, some of the
strange enum rules begin to make sense.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.