Why does a sub-class class of a class have to be static in order to initialize the sub-class in the constructor of the class?

后端 未结 2 674
情话喂你
情话喂你 2020-12-28 19:22

So, the question is more or less as I wrote. I understand that it\'s probably not clear at all so I\'ll give an example.

I have class Tree and in it there is the cla

2条回答
  •  温柔的废话
    2020-12-28 19:48

    Good news! The sub-class class of an inner class does NOT have to be static!

    Here is a technique explained by Henry Wong at code ranch that works for outer classes that subclass inner classes. It worked well for me and it's always fun to see how the language designers had to contort Java to handle the corner cases :)

    http://www.coderanch.com/t/588820/java/java/Extend-class-code-top-level#2681401

    Here's the example:

    class Demo extends Main.Inner{
        public Demo(Main outer) {
            outer.super();
        }
    
        void method(){
            System.out.println(a);
        }
    }
    

提交回复
热议问题