Skip to content

Instantly share code, notes, and snippets.

@kabutz
Created November 4, 2016 20:55
Show Gist options
  • Save kabutz/eed6e7899cec8f4394a8bfd35081645b to your computer and use it in GitHub Desktop.
Save kabutz/eed6e7899cec8f4394a8bfd35081645b to your computer and use it in GitHub Desktop.
Strange class that compiled with JDK 9, but not 1.6, 1.7 nor 1.8.
/**
* As seen on https://javaspecialists.slack.com/messages/general
*
* This class fails to compile with JDK 1.6, 1.7 and 1.8:
*
* class Two extends Observable { // Nothing special about the choice of Observable here
* ^
* symbol: class Observable
* 1 error
*
* It does compile with JDK 9 even with the -source 1.8 -target 1.8 flags. If we change
* the order of the imports and put the "import static" after the normal import, it
* compiles in all JDK versions. IDEs usually reorder these imports automatically, which
* is why you might not have seen this compiler error. We would have thought that it
* didn't matter.
*
* Survey results:
* ===============
* 18% said "static imports first", which makes javac unhappy
* 24% said "static imports last", which is correct
* 52% said "Compiler doesn't care". That's what I would've answered too, but as we
* see in the code example, javac /does/ care. IMHO it shouldn't. Fixed in JDK 9.
* 6% said "I know how to fix that..." - perhaps they knew about javac in JDK 9?
*
* I would say that at least 70% were incorrect. Amazing how IDEs have made our brains
* switch off. And with Apple removing the Esc key, who will be using VIM in future?
*
* @author Tom Gutteridge, Heinz Kabutz
*/
package whatever;
import static whatever.Two.CONSTANT;
import java.util.Observable;
public class One {
int blah = CONSTANT; // Just so there's some point in the static import
}
class Two extends Observable { // Nothing special about the choice of Observable here
static final int CONSTANT = 123;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment