Java 参数计数的非法修饰符;只允许final
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23012022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal modifier for parameter count; only final is permitted
提问by user3421750
I new to Java and I have a problem, When i try making a public/private variable
EXAMPLE:
private int varName;Eclipse gives me an error:
Illegal modifier for parameter count; only final is permitted
我是 Java 新手,但遇到了问题,当我尝试创建公共/私有变量时:
private int varName;Eclipse 给了我一个错误:
Illegal modifier for parameter count; only final is permitted
采纳答案by Rohit Jain
Local variables and parameters cannot have publicor privatemodifier. You can only give finalto them. Not even staticcan be used.
局部变量和参数不能有publicorprivate修饰符。你只能给final他们。甚至static不能使用。
回答by Marko Topolnik
You cannot apply access-level modifiers on method parameters. They are acceptable only on class members. Furthermore, that wouldn't make any sense because a parameter cannot possibly be accessed outside of the method scope.
您不能对方法参数应用访问级别修饰符。它们仅适用于班级成员。此外,这没有任何意义,因为不可能在方法范围之外访问参数。
回答by Raju Sharma
This happens generally whenever we try to access variable which is local and we try to access it in Anonymous class methods like below:
每当我们尝试访问本地变量并尝试在匿名类方法中访问它时,通常会发生这种情况,如下所示:
JButton button=new JButton();
int a=5;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println(""+a);//Compiler Error:Cannot refer to a non-final variable a inside an inner class defined in a different method
}
});
so here the variable "a" need to be final or Class variable to be accessed inside the Anonymous class method.
所以这里的变量“a”需要是最终的或类变量才能在匿名类方法中访问。

