java – Reflection Class. Forname() looks for classes $1 and $2. What are they?

See English answers > java compiled classes contain dollar signs

When checking the string input userinput, see if a class with that name exists:

Class<?> c = Class.forName(this.getClass().getName() + "$" + userInput);

When the user enters "1" or "2", what surprises me is class Forname() did find a class with that name This may be basic Java, so please forgive me: what are these classes? I use the debugger to trace the code and check other numbers. It seems that only 1 and 2 are defined

Solution

Those are anonymous inner classes For example:

public class Foo {
    public static void bar() {
        Runnable runnable = new Runnable() {
            @Override public void run() {}
        };
    }
}

This will create a class foo $1 that implements runnable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>