在Java 9的JShell中,不能在顶层声明中使用哪些修饰符?

在Java 9的JShell中,不能在顶层声明中使用哪些修饰符?

像public、protected、private、static和final 这样的修饰符不允许在顶层声明中使用,并且可以被忽略并显示一个警告。像synchronized、native、abstract和default top-level方法这样的关键字不允许使用,并且可能会引发错误。

在下面的代码片段中,我们创建了final 和static 变量。它向用户打印一个警告消息,内容为“Modifier 'final' or 'static' not permitted in top-level declarations, ignored”。

Example-1

C:UsersUser>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> final int x = 0 | Warning: | Modifier 'final' not permitted in top-level declarations, ignored | final int x = 0; | ^---^ x ==> 0 jshell> x = 1 x ==> 1登录后复制