简体   繁体   中英

What is a shadow exactly, in static analysis of code?

I tried looking for the answer to this! There are a lot of papers that say about Shadows. What is this exactly?

Assuming you are talking about shadowing with names, the Java Language specification says this

Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.

and gives this example

class Test {
    static int x = 1;
    public static void main(String[] args) {
        int x = 0;
        System.out.print("x=" + x);
        System.out.println(", Test.x=" + Test.x);
    }
}

where x is a static class variable and a local variable. The local variable will be used if x is referenced in the method the local variable x is defined in. If you wanted to reference the class variable, you would need to use

Test.x

Analysis tools can find things like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM