ต่อไปนี้คือตัวอย่าง ซึ่งเราจะเห็นคำสั่งที่ไม่สามารถเข้าถึงได้โดยใช้ตัวแปรที่ไม่สิ้นสุด -
ตัวอย่าง
class Demo_example {
int a = 2, b = 3;
void display_msg(){
while (a < b){
System.out.println("The first variable is greater than the second");
}
System.out.println("This is an unreachable statement");
}
}
public class Demo{
public static void main(String args[]){
Demo_example my_instance = new Demo_example();
my_instance.display_msg();
}
} ผลลัพธ์
“The first variable is greater than the second” displayed infinitely
คลาสชื่อ Demo_example ที่กำหนดตัวแปรสองตัว จากนั้นจะมีการกำหนดฟังก์ชันชื่อ 'display_msg' และตัวแปรทั้งสองจะถูกตรวจสอบความเท่าเทียมกัน ข้อความที่เกี่ยวข้องจะแสดงบนคอนโซล ฟังก์ชันอื่นที่ชื่อว่า 'Demo' มีฟังก์ชันหลัก ซึ่งอินสแตนซ์ของคลาส 'Demo_example' จะถูกสร้างขึ้น อินสแตนซ์นี้เรียก "display_msg" และผลลัพธ์ที่เกี่ยวข้องจะแสดงบนคอนโซล