Skip to content
Advertisement

Can anyone explain this code related to shadowing in Java?

While going through Oracle docs reading about Nested classes, I found this piece of code whose output I could not understand. Can someone please explain this ?

JavaScript

The following is the output of this example:

JavaScript

The original code can be found here

Advertisement

Answer

The local variable x shadows this.x and ShadowTest.this.x.

The instance variable of the inner class (this.x) shadows the instance variable of the enclosing class (which can be accessed by ShadowTest.this.x).

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement