Which modifiers can't allow in the top-level declaration in JShell in Java 9?


JShell is an interactive tool for learning the Java language and prototyping Java code. It is a REPL (Read-Evaluate-Print-Loop) that evaluates declarations, statements, and expressions once entered and immediately prints the results in JShell. This tool runs from the command-line prompt.

The modifiers like public, protected, private, static, and final have not allowed on top-level declarations and can be ignored with a warning. The keywords like synchronized, native, abstract, and default top-level methods have not allowed and can be errors.

In the below code snippets, we have created both final and static variables. It prints out a warning message to the user that "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

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

raja
raja

e

Updated on: 01-Apr-2020

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements