0

几周前我刚刚开始学习 Java。我正在尝试学习java语言中的关键字final。我final string在方法中写了一个声明public static void main。但是,IDE 显示错误:非法启动表达式。但是,IDE 没有提供任何其他信息。因为我对 Java 比较陌生,所以我无法理解为什么会发生这种情况。有人可以向我解释原因吗?

public static void main(String[] args) {
    private final String s1 = "hello world";
}
4

1 回答 1

1

There is no such thing as a private field within a method, and this only makes sense within class scope, not method scope. Get rid of the private modifier.

To get a handle on this, ask yourself, what would it mean? The variable s1 is local to he method regardless and so cannot be seen outside of the method scope, so adding a private modifier would be senseless as it wouldn't change anything.

于 2016-03-02T22:58:59.617 回答