Kushagra
Kushagra

Reputation: 89

Java program had compilation error yet ran successfully

class Return
{

      public static void main(String args[])
      {
         boolean t=true;
         System.out.println("Before the return");

         if(t)
         return;

         System.out.println("This wont execute");   
      }
}

This program is from Herbert Schidt. I tried to run this program using command prompt without if(t) to see the compilation error

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

I understood this error but program is running fine when i execute the command java Return. It shows the output

Executes

So I wanted to know how this program is running even with compilation error?

Upvotes: 0

Views: 123

Answers (1)

Todd
Todd

Reputation: 31710

You were almost certainly running the last successfully compiled version of this class. When you execute the java compiler, it doesn't erase its old output, it overwrites it. So if you compiled Return.java once successfully, you'll have a Return.class on your disk. If you alter Return.java to be uncompilable and try to compile it, the Return.class from the previous successful compile will still be there.

Upvotes: 3

Related Questions