[Logo]
Forums Register Login
Anonymous class with the same method name as the enclosing class
Is the following code valid according to the Java specification:



Eclipse's compiler says it's a compile-time error but Oracle's compiler thinks it's okay. The same difference even if I change the source property (i.e. -source 1.5 or 1.7) in both. Are different compilers generally allowed to produce different results? Or is the Eclipse compiler not standard compliant (or the other way around)?

I know I can fix this by writing

but I'm just curious what's going on.
 

Hopea Tassu wrote:Is the following code valid according to the Java specification:



Eclipse's compiler says it's a compile-time error but Oracle's compiler thinks it's okay. The same difference even if I change the source property (i.e. -source 1.5 or 1.7) in both. Are different compilers generally allowed to produce different results? Or is the Eclipse compiler not standard compliant (or the other way around)?

I know I can fix this by writing

but I'm just curious what's going on.



Curious, I did some testing, and it seems that method overloading is a little tricky this way. To be honest, I think that method overloading this way is just not possible.
Thank you for your input. I just know that it works with at least JDK 1.7.0_03 from Oracle. I stumbled upon this problem when I had a project in Netbeans and switched to Eclipse and it wouldn't compile there.
 

Jim Pouwels wrote:Curious, I did some testing, and it seems that method overloading is a little tricky this way. To be honest, I think that method overloading this way is just not possible.


There is no method overloading going on in this example. Note that the foo(int) and foo(int, int) methods are defined in two different classes: class B and class Test, and these classes are not related to each other in an inheritance hierarchy. So the two foo's are just two different methods in different classes.

How are your Eclipse settings; are the compiler compliance settings set to Java 7 compatibility?

If yes, and one compiler gives and error while the other doesn't, then it's a bug in one of the compilers.
 

Jesper de Jong wrote:
How are your Eclipse settings; are the compiler compliance settings set to Java 7 compatibility?


Hello. I've made sure that the JDK compliance in Eclipse is set to 1.7. But now that I've investigated the issue more, I think it's Oracle's compiler that is faulty.

The Java specification says:

A declaration d of a method named n shadows the declarations of any other methods named n that are in an enclosing scope at the point where d occurs throughout the scope of d.


And method name is defined to be only the identifier without the argument list if I understood correctly.

Furthermore, the code compiles only if the method is overridden and not a completely new definition. This works:


but if I remove "extends B", then I get the error "actual and formal argument lists differ in length".
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 620 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 19, 2018 21:56:52.