Java Final变量是否具有默认值? [英] Will Java Final variables have default values?
问题描述
我有这样的程序:
class Test {
final int x;
{
printX();
}
Test(){
System.out.println(const called);
}
void printX(){
System.out.println(这里x是+ x);
}
public static void main(String [] args){
Test t = new Test();
}
}
如果我尝试执行它,我收到编译器错误:变量x可能尚未初始化
基于java默认值我应该得到以下输出吗?
这里x为0。
最终变量是否具有dafault值?
如果我这样更改我的代码,
class Test {
final int x;
{
printX();
x = 7;
printX();
}
Test(){
System.out.println(const called);
}
void printX(){
System.out.println(这里x是+ x);
}
public static void main(String [] args){
Test t = new Test();
}
}
我输出为:
这里x是0
这里x是7
const叫做
任何人都可以解释这种行为..
http:// docs。 oracle.com/javase/tutorial/java/javaOO/initial.html ,初始化实例成员一章:
Java编译器将初始化程序块复制到每个构造函数中。
也就是说:
< pre class =lang-java prettyprint-override>
{
printX();
}
Test(){
System.out.println(const called);
}
表现如下:
Test(){
printX();
System.out.println(const called);
}
正如您可以看到的,一旦创建了实例,最后的字段尚未 明确分配 ,而(来自 http://docs.oracle.com/javase/ specs / jls / se7 / html / jls-8.html#jls-8.3.1.2 ):
空白决赛实例变量必须在
处明确赋值,该类的每个构造函数的末尾都是
声明的;否则会发生编译时错误。
虽然在文档中似乎没有明确说明(至少我没有能够找到它),一个final字段必须临时在构造函数结束之前取其默认值,以便它有一个可预测值 ,如果您在分配之前阅读它。
默认值: http://docs.oracle.com/javase/specs/jls/ se7 / html / jls-4.html#jls-4.12.5
在你的第二个片段中,x在实例创建时被初始化,所以编译器会不要抱怨:
Test(){
printX();
x = 7;
printX();
System.out.println(const called);
}
另请注意以下内容方法不起作用。使用final变量的默认值只能通过方法。
Test(){
System.out.println(这里x是+ x); //编译时错误:变量'x'可能未初始化
x = 7;
System.out.println(这里x是+ x);
System.out.println(const called);
}
I have a program like this:
class Test {
final int x;
{
printX();
}
Test() {
System.out.println("const called");
}
void printX() {
System.out.println("Here x is " + x);
}
public static void main(String[] args) {
Test t = new Test();
}
}
If I try to execute it, i am getting compiler error as : variable x might not have been initialized
based on java default values i should get the below output right??
"Here x is 0".
Will final variables have dafault values?
if I change my code like this,
class Test {
final int x;
{
printX();
x = 7;
printX();
}
Test() {
System.out.println("const called");
}
void printX() {
System.out.println("Here x is " + x);
}
public static void main(String[] args) {
Test t = new Test();
}
}
I am getting output as:
Here x is 0
Here x is 7
const called
Can anyone please explain this behavior..
http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html, chapter "Initializing Instance Members":
The Java compiler copies initializer blocks into every constructor.
That is to say:
{
printX();
}
Test() {
System.out.println("const called");
}
behaves exactly like:
Test() {
printX();
System.out.println("const called");
}
As you can thus see, once an instance has been created, the final field has not been definitely assigned, while (from http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.2):
A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.
While it does not seem to be stated explitely in the docs (at least I have not been able to find it), a final field must temporary take its default value before the end of the constructor, so that it has a predictable value if you read it before its assignment.
Default values: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5
On your second snippet, x is initialized on instance creation, so the compiler does not complain:
Test() {
printX();
x = 7;
printX();
System.out.println("const called");
}
Also note that the following approach doesn't work. Using default value of final variable is only allowed through a method.
Test() {
System.out.println("Here x is " + x); // Compile time error : variable 'x' might not be initialized
x = 7;
System.out.println("Here x is " + x);
System.out.println("const called");
}
这篇关于Java Final变量是否具有默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!