Unreachable statement using the non-final variable in Java


Following is an example, wherein we will see unreachable statement using the non-final variable −

Example

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();
   }
}

Output

“The first variable is greater than the second” displayed infinitely

A class named Demo_example, that defines two variables. Then a function named ‘display_msg’ is defined, and the two variables are checked for their equality. Relevant message is displayed on the console. Another function named ‘Demo’ contains the main function, where an instance of the ‘Demo_example’ class is created. The ‘display_msg’ is called on this instance and the relevant output is displayed on the console.

Updated on: 17-Aug-2020

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements