[英]Why can't I create static final variable in psvm Java?
It will not compile but when I put initalization of variable out of main method it will be ok. 它不会编译,但是当我将变量的初始化放在主要方法之外时就可以了。
public class Demo {
public static void main(String[] args) {
static final int x = 2;
System.out.println(x);
}
}
static final int x = 2;
local variables can't be static that's why compiler complains 局部变量不能是静态的,这就是编译器抱怨的原因
static
modifier is who is causing problem here. static
修饰符是谁在这里引起问题。 you cant make a local variable static
for obvious reasons. 由于明显的原因,您不能将局部变量static
。
不能将局部变量声明为private,public,protected或static ,请删除static
关键字。
static
variable is loaded when a class gets loaded. 加载类时将加载static
变量。 But a local variable gets loaded when method gets called (after class gets loaded). 但是在调用方法时(在类加载之后),将加载局部变量。 So when class gets loaded , local variable will not be created by that time 因此,当类被加载时,那时将不会创建局部变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.