JShell is an interactive tool for learning the Java language and prototyping Java code. It is a REPL (Read-Evaluate-Print-Loop) that immediately evaluates statements, statements and expressions## once entered. #, and print the results immediately in JShell. This tool is run from the command line prompt.
Modifiers likepublic, protected, private, static, and final Not allowed in top-level declarations, and may be ignored with a warning. Keywords like synchronized, native, abstract, and default top-level methods are not allowed and may raise mistake.
In the code snippet below, we createfinal and static variables. It prints a warning message to the user that reads "Modifier 'final' or 'static' not permitted in top-level declarations, ignored". The Chinese translation of
Example-1<strong>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</strong>
Example-2
Example-2
<strong>jshell> static String str = "Tutorix" | Warning: | Modifier 'static' not permitted in top-level declarations, ignored | static String str = "Tutorix"; | ^----^ str ==> "Tutorix" </strong>
The above is the detailed content of What modifiers cannot be used in top-level declarations in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!