I tried to replicate the issue you are facing by following the below steps.
- I have created a java file "ShadowTest.java" with your code.
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
Compiled the ShadowTest.java using javac ShadowTest.java, two class files were created
D:\test>javac ShadowTest.java
D:\test>dir
Volume in drive D is MonWork
Volume Serial Number is 3A72-52A4
Directory of D:\test
17-07-2021 13:01 .
17-07-2021 13:01 ..
17-07-2021 13:01 901 ShadowTest$FirstLevel.class
17-07-2021 13:01 529 ShadowTest.class
17-07-2021 13:01 539 ShadowTest.java
3 File(s) 1,969 bytes
2 Dir(s) 55,102,857,216 bytes free
output of javac command and newly created class files in the same directory
On executing ShadowTest class, with java ShadowTest command. I am getting the expected output.
OUTPUT:
D:\test>java ShadowTest
x = 23
this.x = 1
ShadowTest.this.x = 0
output of java command and its output
To reproduce your issue I tried to delete ShadowTest$FirstLevel.class file and execute java ShadowTest. But did not get the exact error you are getting.
Error: Could not find or load main class . Caused by: java.lang.ClassNotFoundException
*D:\test>del ShadowTest$FirstLevel.class
D:\test>java ShadowTest
Exception in thread "main" java.lang.NoClassDefFoundError: ShadowTest$FirstLevel
at ShadowTest.main(ShadowTest.java:18)
Caused by: java.lang.ClassNotFoundException: ShadowTest$FirstLevel
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more*
executing main class after deleting the class file for the inner class.
Please share the complete error message and commands you are using for compiling and executing the class.