Instantiate nested static class using Class.forName

后端 未结 4 768
北恋
北恋 2020-12-28 11:30

I have a nested static class like:

package a.b
public class TopClass {

    public static class InnerClass {
    }
}

I want to

4条回答
  •  余生分开走
    2020-12-28 12:11

    Nested classes use "$" as the separator:

    Class.forName("a.b.TopClass$InnerClass");
    

    That way the JRE can use dots to determine packages, without worrying about nested classes. You'll spot this if you look at the generated class file, which will be TopClass$InnerClass.class.

    (EDIT: Apologies for the original inaccuracy. Head was stuck in .NET land until I thought about the filenames...)

提交回复
热议问题