I’m experiencing some strange behavior when using static imports of inherited static methods:
com/example/util/BaseUtil.java:
package com.example.util;
/*default*/ class BaseUtil {
public static final void foo(){ System.out.println("foo"); }
}
com/example/util/Util.java:
package com.example.util;
public final class Util extends BaseUtil{
public static void bar(){ System.out.println("bar"); }
//foo() will be inherited
}
com/example/UtilTest.java
package com.example;
import static com.example.util.Util.bar;
import static com.example.util.Util.foo;
public class UtilTest {
public static void main(String[] args) {
bar();
foo();
}
}
Running UtilTest result in an unchecked exception!
Exception in thread "main" java.lang.IllegalAccessError: tried to access class com.example.util.BaseUtil from class com.example.UtilTest
at com.example.UtilTest.main(UtilTest.java:15)
However, if I were to reference the methods via Util (without static imports) everything works as expected:
com/example/UtilTest.java
package com.example;
import com.example.util.Util;
public class UtilTest {
public static void main(String[] args) {
Util.bar();
Util.foo();
}
}
So, what gives?
That class has defualt access specifier, which makes it invisible from outside of that package.
You need to make it public.
Update
Following is how the decompilation looks like:
And the following is what I get by using JD GUI
which of course is not going to compile.
Looks like a hole in the compiler (may be due to the static imports) here.