问题描述
我正在使用JDK9的jshell。
我刚刚创建了一个最终变量并为其分配了一个值。
在下一行中,我刚刚修改了值。令我惊讶的是,修改最终变量时没有错误。
以下是代码段:
jshell>最终整数r = 0;
|警告:
|
|顶级声明中不允许使用修饰符 final最终整数r = 0;
| ^ --- ^
r ==> 0
jshell> r = 1;
r ==> 1
jshell> System.out.println( r = + r)
r = 1
是jshell会带来什么?还是有其他方法可以在jshell中使用最终变量?
在顶层创建最终变量的同时不应该练习。但是我想并没有限制这种用法的硬方法。
来自
I am working with jshell of JDK9.
I just created a final variable and assigned a value to it.And in the next line i just modified the value. And to my surprise, there was no error when modifying the final variables.
Here is the code snippets:
jshell> final int r = 0;
| Warning:
| Modifier 'final' not permitted in top-level declarations, ignored
| final int r = 0;
| ^---^
r ==> 0
jshell> r = 1;
r ==> 1
jshell> System.out.println("r = "+r)
r = 1
Is it what is expected from jshell? or there is some other way to work with final variables in jshell?
While creating a final variable at the top-level is not supposed to be practiced. But I guess there is no hard way of restricting such usages.
From the documentation around JShell.eval
The warning stated above is quite visible when you execute jshell
in verbose mode as follows:
这篇关于最终变量在jshell中运行不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!