Disambiguation of the static method

I am working on understanding java compilation and I came across a question. Say we have a class like this

public class Ambiguity 
{
    static class G
    {
        void A()
        {
            System.out.println("Regular Method");
        }
    }

    static class b
    {
        static void A()
        {
            System.out.println("Static Method");
        }
    }

    public static void main(String[] args)
    {

        G b = new G();
        b.A();
    }
}

How does the compiler know the weather to call a method static void A()in class bor a regular method of an void A()object of btype G. Based on the tests, it calls a method b(type G) call , but I did not know if this is a standard procedure. In this situation, you can call the static method.

+4
source share
3 answers
  • Local variable with name band type G,
  • "" b , b .
  • b .

, . , JLS §6.4.2:

, , . §6.5 , , . , . , .

, , . , (§6.2). §6.5:

G b = new G();
Ambiguity.b.A(); // calls Ambiguity.b.A()
b.A();           // calls Ambiguity.G.A() on variable b

Static Method
Regular Method

~~~~

, b (§6.5.1). §6.5.2 ( , ))

AmbiguousName ,

(§6.3) (§14.4) (§8.4.1, §8.8.1, §14.20) (§8.3) , AmbiguousName ExpressionName (... a TypeName, 3).

+6

" ". , , ; Class.Method();. , , b , , , , .

, : Ambiguity.b.A();

+1

:

Ambiguity.b.A();

Java . Ambiguity.b , , .

0

All Articles