Access to a shadow variable from an anonymous class

I have a static function that takes an X parameter.

In this static function, I create an anonymous class. This class must have a member variable, also called X.

From functions in an anonymous class, how can I access the parameter of function X?

To illustrate:

class Test {

    static void func(final List<T> X) {

        new Test() {
            final T[] X = ?.X.toArray();
        };
    }
}

In my real code, I want to create an array in my anonymous class from a list argument (through toArray()in the initialization of an anonymous class), and I want to reuse the same variable name, and not use the Hungarian notation to differentiate them.

+3
source share
1 answer

Java. " ", , this, .

- outerX xAsArray .

+5

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


All Articles