Instantiate nested static class using Class.forName

后端 未结 4 734
北恋
北恋 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:19

    Inner class is always accessed via dollar sign because when java compiler compile the java source code file it generates .class file(byte code).

    if there is only one class for example Hello.java and this class is an outer class then java compiler on compilation generate Hello.class file but if this class has an inner class HelloInner then java compiler generates d Hello$HelloInner.class(byte code).

    so bytecode always looks like for following snippet with name Outer.java:

       public class   Outer
       {
         public  var;//member variable
           Outer()//constructor
           {
            }
           class  Inner1
            {
              class Inner2
                 {  
                  }
             }
           }
    

    so byte code is:Outer$Inner1$Inner2.class

    that's why we use $ sign to access inner class .:)

提交回复
热议问题