Java中使用非final变量的不可访问语句
下面是一个示例,其中我们将看到使用非final变量“-的unreachable语句
示例
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”,并且相关输出显示在控制台上。