How to switch off "java: variable might not have been initialized"

From the javadoc of System.exit:

Terminates the currently running Java Virtual Machine.

Your compiler does not look ahead to figure out that the jvm will close and therfore the program will quit.

If you change System.exit(1); with return;, the compiler will recognize that at this point, your will return the control flow to the calling method. Since you are in the main method of the Main class of your program, it will terminate the program as well. Only now, your compiler knows about it.

You can use the following code, but be aware, that you will not send a status code 1 to your operating system in this case:

public static void main( String[  ] args ) {

try{
    final int begin = Integer.valueOf( args[ 1 ] );
    final int end = Integer.valueOf( args[ 2 ] );

    if( begin >= end ) {
        System.out.println( "Wrong arguments. (" + begin + " >= " + end + ")" );
        System.exit(1);
    }

} catch( NumberFormatException conversion_error ) {
    System.out.println( "Not A Number." );
    return;
}

System.out.print( "OK." );
System.exit(0);
}

No. You can't switch it off. The compiler insists that you don't use uninitialized variables. It is a rule of Java.

You need to fix your code. Specifically, code that depends on the success of code in a prior try block should be inside that try block.

Tags:

Java