Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse loves it, javac hates it, it's an enum, sort of, with an interface

Eclipse indigo, java 1.6

public interface I {
  String getName();
}

/* and in another file */

public enum E implements I {
  E1() {
     String getName() { return "foo"; }
  };
}

In Eclipse, this worked! Other classes could invoke getName() on references of type I. The actual javac rejected it, claiming that there was no such thing as getName() in the enum. Is this just an Eclipse bug?

Note that what's wierd about this is the method definition inside the enumerator. It all works just fine in both Eclipse and Javac if I do the normal thing, and have the function defined at the bottom of the enum returning the value of a field.

like image 458
bmargulies Avatar asked Mar 07 '12 18:03

bmargulies


3 Answers

getName() in E1 should be public -- is that what's causing you problems? Otherwise, you're trying to override a public method (all methods declared in interfaces are public) with a package-private method, which isn't allowed.

like image 157
yshavit Avatar answered Nov 17 '22 08:11

yshavit


First I agree with @yshavit.

Otherwise it can be related with this one: Workaround for javac compilation order bug in maven

I think it's name order related. Try to rename your interface A, it may compile first and things should work.

like image 1
Nicocube Avatar answered Nov 17 '22 08:11

Nicocube


Interfaces methods are public scoped. Increase the visibility level in your enum and it should compile successfully. As a side note, your code shows a compilation error in my version of Eclipse (Indigo running on Mac 0S X 10.7.2, JDK 1.6).

like image 1
Perception Avatar answered Nov 17 '22 07:11

Perception