Skip to content
Related Articles

Related Articles

Unreachable statement using final and non-final variable in Java
  • Difficulty Level : Easy
  • Last Updated : 10 Dec, 2018

Unreachable statements:
The statements that won’t get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons:

  1. Have a return statement before them
  2. Have a infinite loop before them
  3. Execution forcibly terminated before them
    1. Example:




      // Java program to show unreachable
      // statement using final variable
      class UnreachableUsingFinal {
          final int a = 14, b = 16; // initialisation of variable
            
          // m1() function of UnreachableUsingFinal class
          void m1()
          {
              while (a < b) {
                  // first print statement
                  System.out.println("Hello i am in infinite loop"); 
              }
        
              // last print statement, in this case unreachable
              System.out.println("Unreachable statement");
          }
      }
      class GfG {
          public static void main(String args[])
          {
              // creating object
              UnreachableUsingFinal uuf = new UnreachableUsingFinal();
               
              // calling m1() function
              uuf.m1();
          }
      }

      
      

      Output:

      error: unreachable statement
           System.out.println("Unreachable statement");
      

      Explanation:
      This is because the statement : System.out.println(“Unreachable statement”); will never be executed and java doesn’t suppport unreachable statements.
      Making those variables final also makes them constant variables and in JAVA or any other programming language the value of the constant variable can’t be changed. So, throughout the life cycle of the program no condition will be approached to come out of the while loop.
      This in turn makes the second statement unreachable i.e. jvm can’t reach to that statement during program life cycle. So, its is giving “unreachable statement” error. Here, the program is right if looked by the coding point of view but what actually JVM thinks is that if in any condition a statement is not executed, then what is the need of using that statement? So, try to avoid this type of situations.

      What if the final keyword is removed in the above program?






      // Java program to execute same program
      // without using final variable
      class UnreachableWithoutUsingFinal {
          // initialisation of variable
          int a = 14, b = 16;
        
          // m1() function of UnreachableWithoutUsingFinal class
          void m1()
          {
              while (a < b) {
                  // first print statement
                  System.out.println("Hello i am in infinite loop");
              }
        
              // last print statement, in this case reachable
              System.out.println("Unreachable statement");
          }
      }
      class GfG {
          public static void main(String args[])
          {
              // creating object
              UnreachableWithoutUsingFinal uwuf = new UnreachableWithoutUsingFinal();
        
              // calling m1() function
              uwuf.m1();
          }
      }

      
      

      Output:

      Hello i am in infinite loop
      Hello i am in infinite loop
      Hello i am in infinite loop
      .
      .
      

      Explanation:
      Here, interesting thing happens. Removing the final keyword prints the output. But in this case also, the last print statement is unreachable.

      So, how is this possible?
      In this program, since the variables are declared as non final. So, the value of these variables can be changed in the program. JVM thinks that at any point of time there is a chance that the change the value of a and b, and the last statement may be executed, so no error is coming in the second program.

      It means that the second program encounters with the infinite loop but there is a chance(as the variables are declared non final) that the loop may terminate and last statement may be executed.

      Note: In the second program, while executing, the output will be TLE as the while loop is not terminating.

      Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

      My Personal Notes arrow_drop_up
Recommended Articles
Page :