Why do I get different compilation result depending on java imports and static imports sequence order? -
i've faced issue compilation, cannot understand why occurs. time spent understand reason (it far obvious in "crap" project), after reproducing error simplifies code show little example you:
package structure:
com.company | ----main.class | ----maker | ----maker.class
maker.class
package com.company.maker; public interface maker { }
main.class
package com.company; import static com.company.main.makerimpl.strategy.strategy1; import static com.company.main.makerimpl.strategy.strategy2; import com.company.maker.maker; public class main { public static void main(string[] args) { system.out.println(strategy1.name() + strategy2.name()); } static class makerimpl implements maker { enum strategy { strategy1, strategy2 } } }
and got compilation error in main class:
error:(15, 39) java: cannot find symbol symbol: class maker location: class com.company.main
and if change import sequence
import static com.company.main.makerimpl.strategy.strategy1; import static com.company.main.makerimpl.strategy.strategy2; ->import com.company.maker.maker;
to
->import com.company.maker.maker; import static com.company.main.makerimpl.strategy.strategy1; import static com.company.main.makerimpl.strategy.strategy2;
then compiled successfully.
is normal behaviour of java compiler? if want understand why happens.
p.s. tested using java version 1.8.0_112 , 1.7.0_80 (macos)
check :
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6391197
it seems compiler sees first static import , jumps take care of inner class, fails because did not read last non static import.
so when change import order, problem not occur, since when compiler reads static import , jumps take care of inner class because imports used in inner class, compiler happy since imported non static maker interface.
wich means imports evaluated eagerly.
i hope helps.
Comments
Post a Comment