JShell là công cụ tương tác để học ngôn ngữ Java và tạo prototype mã Java. Nó là REPL (Read-Evaluate-Print-Loop) đánh giá khai báo, câu lệnh và biểu thức ngay khi nhập và in kết quả ngay lập tức trong JShell. Công cụ này chạy từ dòng lệnh.
Các modifier như public, protected, private, static và final không được phép trên khai báo top-level và sẽ bị bỏ qua với cảnh báo. Các từ khóa như synchronized, native, abstract và default top-level methods không được phép và sẽ báo lỗi.
Trong các đoạn mã dưới đây, chúng ta tạo cả biến final và static. Nó in ra thông báo cảnh báo: "Modifier 'final' or 'static' not permitted in top-level declarations, ignored".
Ví dụ 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
Ví dụ 2
jshell> static String str = "Tutorix"
| Warning:
| Modifier 'static' not permitted in top-level declarations, ignored
| static String str = "Tutorix";
| ^----^
str ==> "Tutorix"