how to call inner class's method from static main() method

前端 未结 3 2009
心在旅途
心在旅途 2020-12-06 13:35

Trying to create 1 interface and 2 concrete classes inside a Parent class. This will qualify the enclosing classes to be Inner classes.

public class Test2 {
         


        
相关标签:
3条回答
  • 2020-12-06 13:46

    Here the inner class is not static, so you need to create an instance of outer class and then invoke new,

    A a = new Test2().new C();
    

    But in this case, you can make the inner class static,

    static class C extends B implements A
    

    then it's ok to use,

    A a = new C()
    
    0 讨论(0)
  • 2020-12-06 13:53

    To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

    OuterClass.InnerClass innerObject = outerObject.new InnerClass();
    

    So you need to use :

    A a = new Test2().new C();
    

    Refer the Java Tutorial.

    0 讨论(0)
  • 2020-12-06 13:55

    You should do this

     A a = new Test2().new C();
    
    0 讨论(0)
提交回复
热议问题