Java 错误 - 参数的非法修饰符 - 仅允许 final
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21280038/
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
Java Error - Illegal Modifier for Parameter - Only final Permitted
提问by user3110336
What's wrong with the below Code
下面的代码有什么问题
public static void main(String[] args){
public static final String Name = "Robin Wilson";
}
String Reference Name shows Compilation Error - Java Error - Illegal Modifier for Parameter Name - Only final Permitted
字符串引用名称显示编译错误 - Java 错误 - 参数名称的非法修饰符 - 只有最终允许
Am okay with the below given suggestions, but I want to understand why its not permitted, though both are static?
可以接受以下给出的建议,但我想了解为什么不允许这样做,尽管两者都是静态的?
回答by Maroun
You can't declare this inside main
, put it outside the method, you want it as a [class member]:
你不能在里面声明它main
,把它放在方法之外,你想要它作为[类成员]:
public static final String Name = "Robin Wilson";
public static void main(String[] args) throws IOException { }
Otherwise(I don't think this is what you want) just remove public static
from there and simply write:
否则(我认为这不是您想要的)只需public static
从那里删除并简单地写:
public static void main(String[] args){
final String Name = "Robin Wilson";
}
回答by Weibo Li
Your can not declare a local variable(variables inside methods are local variables) as public static
. Instead, the following code will work:
您不能将局部变量(方法内部的变量是局部变量)声明为public static
. 相反,以下代码将起作用:
public static void main(String[] args){
final String Name = "Robin Wilson";
}
回答by stinepike
you can not use public static modifier for a local variable. Do any of the followings
您不能对局部变量使用 public static 修饰符。执行以下任一操作
public static void main(String[] args){
final String Name = "Robin Wilson";
}
or declare it as a member variable
或将其声明为成员变量
public static final String Name = "Robin Wilson";
public static void main(String[] args){
}
Remember that the final is the only modifer of the local variables
记住 final 是局部变量的唯一修饰符
回答by Amrendu Pandey
as you are Declaring the String variable as public static final String Name = "Robin Wilson";
正如您将 String 变量声明为public static final String Name = "Robin Wilson";
According to java rules, This String nameis Local variable as you are declaring it in Mainmethod. So only final is permitted here. you can define it as ** final String name="Robin Wilson";** in main method. for Local variablesonly finalis permitted.
根据 java 规则,此字符串名称是本地变量,因为您在Main方法中声明它。所以这里只允许final。您可以将其定义为 ** final String name="Robin Wilson";** 在 main 方法中。对于局部变量,只允许使用final。
回答by Brian Roach
Your have modified your question to ask:
你已经修改了你的问题来问:
I want to understand why it is not permitted, though both are static?
我想了解为什么不允许这样做,尽管两者都是静态的?
Variables inside a method exist only on the stack frame. The JVM creates a new stack frame every time a method is invoked, and it is discarded once the method completes.
方法内的变量仅存在于堆栈框架中。每次调用方法时,JVM 都会创建一个新的堆栈帧,并在方法完成后将其丢弃。
The public
keyword is used on classes, methods and fields to control access. There is no concept of access that could be applied to a stack (local) variable. It only exists inside the method when it's called, and can only be accessed from within the method.
该public
关键字用于类、方法和字段以控制访问。没有可以应用于堆栈(局部)变量的访问概念。它仅在调用时存在于方法内部,并且只能从方法内部访问。
The static
keyword is used on fields to denote that only one such member exists across all instances of a class, and on methods to create them as members of the class that do not require an instance. There is no concept of a static state for anything on the stack; it is temporary. The stack frame and all the local variables on it cease to exist once you return from a method call.
该static
关键字用于上的字段来表示只有一个这样的成员跨类的所有实例存在,并且方法来创建他们作为不需要一个实例类的成员。堆栈上的任何东西都没有静态的概念;这是暂时的。一旦从方法调用返回,堆栈帧及其上的所有局部变量就不再存在。
Basically, neither make any sense when talking about a local variable.
基本上,在谈论局部变量时两者都没有任何意义。
回答by codechefvaibhavkashyap
Static declaration of a component makes in available on a class level. Declaring component in a method makes in available in method's stack memory hence can only be accessed through an object. Static belongs to the whole class. Therefore their is no point in declaring a variable static. You'll even get the compile time error if you try to do so.
Final keyword has nothing related to memory.
组件的静态声明在类级别上可用。在方法中声明组件可以在方法的堆栈内存中使用,因此只能通过对象访问。静态属于整个类。因此,他们没有必要声明一个静态变量。如果您尝试这样做,您甚至会收到编译时错误。
final 关键字与内存无关。
回答by Nathan Hinchey
How to fix it
如何修复
public
and static
cannot be used inside a methoddefinition. So this error is telling you that the only modifier allowed for a variable defined inside of a methodis final
.
public
并且static
不能在方法定义中使用。所以这个错误告诉你,在方法内部定义的变量允许的唯一修饰符是final
.
You fix it by either by removing the offending modifiers:
您可以通过删除有问题的修饰符来修复它:
class MyClass
{
public static void main(String[] args){
final String Name = "Robin Wilson";
}
}
or moving the variable definition out of the method like this
或者像这样将变量定义移出方法
class MyClass
{
public static void main(String[] args){
}
public static final Name = "Robin Wilson";
}
Explanation
解释
To understand why, you need to understand what each of the three modifiers (public
and static
and final
) means on its own. String Name
just says that we are keeping track of a String
and calling it Name
.
要了解原因,您需要了解三个修饰符(public
andstatic
和final
)各自的含义。String Name
只是说我们正在跟踪 aString
并调用它Name
。
public
民众
class MyClass
{
public String Name = "Robin Wilson";
}
public
says that any part of the program can read it (otherwise it could only be read by code written in the MyClass
class).
public
表示程序的任何部分都可以读取它(否则只能由编写在MyClass
类中的代码读取)。
public
specifies what other code has access to it. Inside a method this doesn't make sense. Variables defined inside methods can only be accessed while inside that method, and once the method is complete they are thrown out. So it would be impossible for those variables to be public
.
public
指定哪些其他代码可以访问它。在方法中,这是没有意义的。方法内部定义的变量只能在该方法内部访问,一旦方法完成,它们就会被抛出。所以这些变量不可能是public
.
static
静止的
class MyClass
{
static String Name = "Robin Wilson";
}
static
says that the Name
variable is a part of the class itself, not of an instance of the class. In other words, all instances of the MyClass
class share the same Name
variable.
static
表示Name
变量是类本身的一部分,而不是类的实例。换句话说,MyClass
类的所有实例共享同一个Name
变量。
static
specifies how it is accessed (either on an instance of the class or through the class itself). Inside a method this doesn't make sense, because local variables are discarded after the method closes, so nothing else will be accessing it.
static
指定如何访问它(在类的实例上或通过类本身)。在方法内部这是没有意义的,因为局部变量在方法关闭后被丢弃,所以没有其他东西可以访问它。
final
最终的
class MyClass
{
final String Name = "Robin Wilson";
}
final
says that the value of Name
will never change once it has been assigned.
final
表示Name
一旦赋值,就永远不会改变。
final
describes how the variable is going to be used. It makes sense within a method, because it is not about access.
final
描述变量将如何使用。它在方法中是有意义的,因为它与访问无关。
回答by ARB
its mean you should write your static function outside the "main" block. It will work fine. Enjoy
这意味着您应该在“main”块之外编写静态函数。它会正常工作。享受