Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Modifier ไหนห้ามใช้ใน Top-level Declaration ของ JShell Java 9

ใน JShell ตัวแปร/เมธอดระดับบนสุด (top-level) ห้ามใช้ modifier บางตัว:

  • Warning (ยอมรับแต่เตือน): public, protected, private, static, final - จะถูก ignore

  • Error (ห้ามใช้): synchronized, native, abstract, default (สำหรับเมธอด top-level)

ตัวอย่าง Warning

jshell> final int x = 0
| Warning: Modifier 'final' not permitted in top-level declarations, ignored
x ==> 0
jshell> x = 1
x ==> 1

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

ตัวแปร final/static ที่ top-level ทำงานได้แต่ได้ warning สามารถเปลี่ยนค่าได้ (ไม่ใช่ final จริง)