Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

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

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 {

       interface A{
             public void call();
       }

       class B implements A{
             public void call(){
                   System.out.println("inside class B");
             }
       }

       class C extends B implements A{
             public void call(){
                   super.call();
             }
       }


       public static void main(String[] args) {
              A a = new C();
              a.call();

       }
}

Now I am not really sure how to create the object of class C inside the static main() method and call class C's call() method. Right now I am getting problem in the line : A a = new C();

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...