在main方法中创建内部类实例时,经常出现以下报错
No enclosing instance of type Single_Producer_Consumer is accessible. Must qualify the allocation with an enclosing instance of type Single_Producer_Consumer (e.g. x.new A() where x is an
instance of Single_Producer_Consumer).
经查证,这是因为内部类是动态的(无static修饰),而main方法是static的,所以创建的内部类也要是static的才行,把内部类改为static的就可以了
本文探讨了在Java中创建内部类实例时遇到的一个常见错误:No enclosing instance of type Single_Producer_Consumer is accessible...。文章解释了这个错误产生的原因在于内部类与静态方法之间的不兼容,并给出了将内部类声明为静态以解决问题的方法。
1万+

被折叠的 条评论
为什么被折叠?



