JShell is a command line tool introduced in Java 9, it is the first Java An official REPL tool for creating a simple programming environment that reads user input, evaluates it, and prints the results.
Declarations outside a class or interface (as well as declarations of classes and interfaces themselves) are created according to the following rules.
External declaration rules:
1) You can ignore public, protected and private etc. access modifier. All declaration fragments are accessible to all other fragments.
<strong>jshell> private int i = 10; i ==> 10 jshell> System.out.println(i); 10</strong>
2) Modifierfinal can be ignored. Allows changes and inheritance.
<strong>jshell> final class A {void m() {} } | Warning: | Modifier 'final' not permitted in top-level declarations, ignored | final class A {void m() {} } | ^---^ | created class A</strong>
3) Modifier static can be ignored because there is no container class.
<strong>jshell> static char letter = 'A; | Warning: | Modifier 'static' not permitted in top-level declarations, ignored | static char letter = 'A'; | ^----^ letter ==> 'A'</strong>
4) Do not allow the modifier's default and synchronization.
<strong>jshell> synchronized void method() {} | Error: | Modifier 'synchronized' not permitted in top-level declarations | synchronized void method() {} | ^----------^</strong>
5) ModifierAbstract Allowed only within classes.
<strong>jshell> abstract void method(); | Error: | Modifier 'abstract' not permitted in top-level declarations | abstract void method(); | ^------^</strong>
The above is the detailed content of What are the rules for external declarations in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!