Java ID Resolution Rules

What search order Foodoes the Java compiler use to resolve in the following class?

class Test
{
  Foo f;
}

Empirical tests show the following search order:

  • Nested Classes
  • Superclass Nested Classes
  • java.lang. * classes
  • Others?

but I would like to know where this is discussed in the Java Language Specification .

+3
source share
2 answers

I do not think that there is a search order in the sense that you have in mind. Rather, I believe this rule applies:

6.5.5 Value of type names The value of a name classified as TypeName is defined as follows.

6.5.5.1 , , . name - .

; . JLS 6.1. Shadowing (JLS 6.3.1), , ( ) , - . , " " (, import java.util.*; java.lang.*) .

, :

package foo;

import java.sql.Date;
import java.util.*;  // import on demand: java.util.Date does not shadow java.sql.Date
import java.awt.*;  // import on demand: java.awt.List does not shadow java.util.List

class Integer { // (foo.)Integer shadows java.lang.Integer
    ...

    static class Integer { // (foo.Integer.)Integer shadows foo.Integer.
        ...

        List t = ... // Compilation error, because there are currently two visible 
                     // declarations of List (JLS 6.5.5.1)
    }
}

, " -" (, import java.sql.Date; ), , , , "type-import" (JLS 6.3.1). , , :

import java.sql.Date;
import java.util.Date;  // Compilation error
+3

, , .

: JVM , , OP Gili

, CW, .

+2

Source: https://habr.com/ru/post/1726876/


All Articles