I have the following simple Java code:

A.java

import static a.A1.*;
import static a.A2.*;
class Main {
    public static int g() {return 999;}
    static {
        System.out.println("f('a'): " + f('a'));
        System.out.println("f(): " + f());
        //System.out.println("g('a'): " + g('a'));
        System.out.println("g(): " + g());
    }
}

a/A1.java

package a;
public class A1 {
    public static int f(char x) {return 1;}
    public static int g(char x) {return 123;}
}

a/A2.java

package a;
public class A2 {
    public static int f() {return 0;}
}

f is defined twice, with overloaded parameters. As you can see, importing both definitions of f statically and calling them works:

$ javac *.java a/*.java && java Main
f('a'): 1
f(): 0
g(): 999
Exception in thread "main" java.lang.NoSuchMethodError: main

But when I defined g locally, as well as statically import it from somewhere, it didn't work. Here is the output when I uncomment line 8 in Main.java:

$ javac *.java a/*.java && java Main
B.java:8: g() in Main cannot be applied to (char)
        System.out.println("g('a'): " + g('a'));
                                        ^
1 error

Why?

I tried changing the local g to non-static, and removing the call to the local g:

import static a.A1.*;
import static a.A2.*;
class Main {
    public int g() {return 999;}
    static {
        System.out.println("f('a'): " + f('a'));
        System.out.println("f(): " + f());
        System.out.println("g('a'): " + g('a'));
    }
}

But it didn't work:

$ javac *.java a/*.java && java Main
B.java:8: g() in Main cannot be applied to (char)
        System.out.println("g('a'): " + g('a'));
                                        ^
1 error
1

There are 1 best solutions below

2
On BEST ANSWER

That is happening because the method named g declared in your class, is shadowing the method named g that is statically-imported on demand. See JLS §6.4.1 - Shadowing:

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.