Ten Kei
Ten Kei

Reputation: 3

Error when executing Java code with nested class

newbie here. Tried executing Java class that has a nested class and I keep getting this Error: Could not find or load main class . Caused by: java.lang.ClassNotFoundException When I compiled the source code, I got two new .class files; ShadowTest$FirstLevel.class and ShadowTest.class. The error shows up when I try to execute either one. Please help.

Here's the 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);
    }
}

Upvotes: 0

Views: 350

Answers (3)

Shashi Shankar
Shashi Shankar

Reputation: 11

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.

Upvotes: 1

saqib kafeel
saqib kafeel

Reputation: 306

use this online compiler ,it's executing your code error free

your code:

enter image description here

output :

enter image description here

Upvotes: 0

Uday Chauhan
Uday Chauhan

Reputation: 1148

You can always compile the class with javac filename command. Then execute the code with command java main_class_name

javac ShadowTest.java
java ShadowTest

/** output **/

x = 23
this.x = 1
ShadowTest.this.x = 0

Inner classes, if any present in your class, will be compiled and the class file will be ClassName$InnerClassName.

Upvotes: 0

Related Questions