Final variables do not function well in jshell

I am working with jshell JDK9.

I just created a final variable and assigned a value to it. And in the next line, I just changed the value. And, to my surprise, there was no error when changing the final variables.

Here are 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 this what is expected from jshell? or is there some other way to work with final variables in jshell?

+4
source share
1 answer

While the creation of the final variable at the top level is not supposed. But I think that there is no difficult way to limit such customs.

From the documentation for JShell.eval

public, protected, private, static final op .

, , .

, , , OVERWRITTEN, , .

, jshell :

enter image description here

+5

Source: https://habr.com/ru/post/1691007/


All Articles