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

王林
王林 转载
2023-08-20 16:25:02 549浏览

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

publicprotectedprivatestaticfinal 这样的修饰符不允许在顶层声明中使用,并且可以被忽略并显示一个警告。像synchronizednativeabstractdefault top-level方法这样的关键字不允许使用,并且可能会引发错误

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

Example-1

C:\Users\User\>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

Example-2

的中文翻译为:

示例-2

jshell> static String str = "Tutorix"
| Warning:
| Modifier 'static' not permitted in top-level declarations, ignored
| static String str = "Tutorix";
| ^----^
str ==> "Tutorix"

以上就是在Java 9的JShell中,不能在顶层声明中使用哪些修饰符?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除