This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Pipeline as Code and have Mohamed Labouardy on-line!
See this thread for details.
Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

java enum with wrong method and compiler behaviour

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why compiler does not throw compilation error for such code??

(you only get compilation error when you try to use getName method somewhere )
 
Saloon Keeper
Posts: 12819
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would it throw an error?
 
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, I also have the same quey and i little modified the existing code

I having Problem with this code is when we uncommented the Line-1 then the Line-2 compile and run but as the is stand there would be a compile error at Line-2. Anyone can explain reason of this??
 
Alexander Shrago
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why would it throw an error?



because in this case getName() is not a jave method
any invocation code will not find out such method in this enum
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are effectively doing is creating anonymous classes extending CountryExternalEnum. In each of those you are declaring a getName() method. That's fine - it's just like adding a method to any other class.

But, because it hasn't been added to the CountryExternalEnum class, you can't use it on a CountryExternalEnum reference, which is why you get the error when you refer to it. You'd need to cast it to the anonymous type first (which you can't do, because it's anonymous!).

On the other hand, uncomment the declaration in CountryExternalEnum, and now the methods in the anonymous classes are overriding this, and calling it on a CountryExternalEnum reference is fine.
 
Alexander Shrago
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashu Mahajan wrote:Hi everyone, I also have the same quey and i little modified the existing code

I having Problem with this code is when we uncommented the Line-1 then the Line-2 compile and run but as the is stand there would be a compile error at Line-2. Anyone can explain reason of this??



I think the method declaration is required. Only then you can override it in some enum members.
But the question is why the compiler allowes such enum with undeclared method?
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matthew Brown,
which anonymous you are talking about i could not understand?? did you mean to say like:
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty much, yes. That's what's happening - that's how you can add/override methods in an individual enum value.

Are you familiar with anonymous classes like this?

Well, that's what's going on in the enum. You can demonstrate this with the following program:

The output is:

So Russia is just an instance of the CountryExternalEnum class. But USA, because you've added something to it, is actually an instance of another class that extends CountryExternalEnum.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic