• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous class with the same method name as the enclosing class  RSS feed

 
Hopea Tassu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hopea Tassu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hopea Tassu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!