本文介绍了Java Final变量是否具有默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有这样的程序: 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 ,初始化实例成员一章:也就是说: { 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 0Here x is 7const calledCan anyone please explain this behavior.. 解决方案 http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html, chapter "Initializing Instance Members":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):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.5On 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变量是否具有默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 02:21