【发布时间】:2014-06-21 13:20:13
【问题描述】:
我试图了解内部类是如何工作的,在尝试一些简单的代码时出现错误:The method hello cannot be declared static; static methods can only be declared in a static or top level type
关于此代码
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
我不明白为什么!
【问题讨论】:
-
我之前读过你的链接:
The main focus is whiteboard questions, problems that you face while in front of your whiteboard designing your project.我认为我的问题不是关于错误本身,而是对成员类如何工作的理解。我一直在等待一个答案,比如“你不能调用内部类而不使它成为静态等”,但最后我认为你是对的,如果我下次遇到这样的问题,我会在 stackoverflow 上发帖:) -
因为它在Java语言规范中是这样指定的。
-
this 可以帮到你
-
谢谢你,我没有注意到我搜索的那个帖子!