Java 中使用非 final 变量的无法访问语句

javaserver side programmingprogramming

以下是示例,我们将看到使用非 final 变量的无法访问语句 −

示例

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’,并将相关输出显示在控制台上。


相关文章