• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are enums implicitly final.  RSS feed

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example from the book "effective java",page no-156.

I just need to know about enum that they are implicitly final or not.as i see in this example the 2 enum constants WEEKENDS and WEEKDAYS are referencing the anonymous enum that must have to extend the PayType in order to override the overtimePay() in PayType.but the book says they are implicitly final.
did i miss something?if i did then please help.
one more thing as the inner enum is private why don't he use the private modifier for the pay() inside it(inner).

Kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by “effectively final”? Do you mean immutable or do you mean that nothing can be overridden in subclasses? Have you seen what it says in the Java® Language Specification?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM link I gave you tells you about the inheritance structure of enums.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What do you mean by “effectively final”? Do you mean immutable or do you mean that nothing can be overridden in subclasses? Have you seen what it says in the Java® Language Specification?

got it ,thank you.
JLS says the enum is implicitly final unless it contains atleast one enum constant that provides a class body and in my case i have 2,indeed.

Thanks again,what about the 2nd question.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the inner enum inherit from, for a start? The private access works the same as private access in any other sort of nested class.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The overtime method would appear to be overriding the abstract method. I think there is something about overriding and abstract methods in the JLS section, too.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The overtime method would appear to be overriding the abstract method. I think there is something about overriding and abstract methods in the JLS section, too.
What does the inner enum inherit from, for a start? The private access works the same as private access in any other sort of nested class.

Yes,JLS says that the enum constant cannot provide a abstract method in its class body,i know about it.
BUT i am asking about the another(2nd one) method,pay(),inside the inner enum.as the inner enum is private it is not accessible outside PayRoll and thus it doesn't makes me sense to provide a package private accessibility to the pay().please help.

And one more thing why he didn't make the inner enum,*PayType*,static.i am understanding it as if i make the inner enum static then then it would make me to use the same 2 instances,WEEKDAYS and WEEKENDS, in the 7 instances or constants of outer enum and their should not be problem like concurrency in sharing those same 2 instances because they are immutable.

Kind Regards,
Praveen.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:BUT i am asking about the another(2nd one) method,pay(),inside the inner enum.as the inner enum is private it is not accessible outside PayRoll and thus it doesn't makes me sense to provide a package private accessibility to the pay().please help.

I wouldn't worry too much about that method's accessibility making sense--the enum type's private scope is the overriding limit to accessibility anyway.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:why he didn't make the inner enum,*PayType*,static.i am understanding it as if i make the inner enum static

See the JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9
The JLS wrote:A nested enum type is implicitly static. It is permitted for the declaration of a nested enum type to redundantly specify the static modifier.

This implies that it is impossible to declare an enum type in the body of an inner class (§8.1.3), because an inner class cannot have static members except for constant variables.

 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I wouldn't worry too much about that method's accessibility making sense--the enum type's private scope is the overriding limit to accessibility anyway.
Thanks junilu,now it makes a sense for me,it will allow me to override the same method in the class body of an enum constant.
JLS wrote wrote:A nested enum type is implicitly static. It is permitted for the declaration of a nested enum type to redundantly specify the static modifier.

I missed it,by mistake.thanks again for letting me know about it.

Praveen.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!