JShell is a command-line tool introduced in Java 9, and it is Java's first official REPL tool to create a simple programming environment that reads the user's inputs, evaluates it, and prints the result.
The declarations outside a class or interface (and declarations of classes and interfaces by themselves) have been created under the following rules.
Rules for External Declarations:
1) Access modifiers like public, protected, and private can be ignored. All declaration snippets can be accessible to all other snippets.
jshell> private int i = 10; i ==> 10 jshell> System.out.println(i); 10
2) The modifier final can be ignored. Changes and inheritance are allowed.
jshell> final class A {void m() {} } | Warning: | Modifier 'final' not permitted in top-level declarations, ignored | final class A {void m() {} } | ^---^ | created class A
3) The modifier static can be ignored because there is not a container class.
jshell> static char letter = 'A; | Warning: | Modifier 'static' not permitted in top-level declarations, ignored | static char letter = 'A'; | ^----^ letter ==> 'A'
4) The modifier's default and synchronized are not allowed.
jshell> synchronized void method() {} | Error: | Modifier 'synchronized' not permitted in top-level declarations | synchronized void method() {} | ^----------^
5) The modifier abstract is allowed only in classes.
jshell> abstract void method(); | Error: | Modifier 'abstract' not permitted in top-level declarations | abstract void method(); | ^------^