Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Các quy tắc cho khai báo external trong JShell Java 9?

JShell là command-line tool giới thiệu trong Java 9, là công cụ REPL chính thức đầu tiên của Java tạo môi trường lập trình đơn giản đọc input user, đánh giá và in kết quả.

Khai báo bên ngoài class hoặc interface (và khai báo class/interface chính chúng) được tạo theo các quy tắc sau.

Quy tắc cho External Declarations:

1) Access modifier như public, protected, private có thể bỏ qua. Tất cả declaration snippet truy cập được lẫn nhau.

jshell> private int i = 10;
i ==> 10
jshell> System.out.println(i);
10

2) Modifier final có thể bỏ qua. Thay đổi và kế thừa được phép.

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) Modifier static có thể bỏ qua vì không có container class.

jshell> static char letter = 'A';
| Warning:
| Modifier 'static' not permitted in top-level declarations, ignored
| static char letter = 'A';
| ^----^
letter ==> 'A'

4) Modifier default và synchronized không cho phép.

jshell> synchronized void method() {}
| Error:
| Modifier 'synchronized' not permitted in top-level declarations
| synchronized void method() {}
| ^----------^

5) Modifier abstract chỉ cho phép ở class.

jshell> abstract void method();
| Error:
| Modifier 'abstract' not permitted in top-level declarations
| abstract void method();
|  ^------^