How to disable java: variable may not have been initialized

I am currently learning Java. I am working on a simple home assistant:

Write a program that receives two digits and a character string from the command line and displays a number-delimited portion of the given string. Handle any exceptions that might occur if the arguments are set incorrectly.

For example: "java Substring Maker 2 4" should result in: "Keg".

So, in case one of the arguments is not a number, I wrote this:

public static void main( String[  ] args ){
    int begin, end;
    try{
        begin = Integer.valueOf( args[ 1 ] );
        end = Integer.valueOf( args[ 2 ] );
    }catch( NumberFormatException conversion_error ){
        System.out.println( "Not A Number." );
        System.exit( 1 );
    }
    if( begin >= end ){
        System.out.println( "Wrong arguments. (" + begin + " >= " + end + ")" );
        System.exit(1);
    }
    System.out.print( "OK." );
    System.exit(0);
}

      

I got it:

Error: (13, 13) java: variable start may not have been initialized Error: (13, 22) java: variable end may not have been initialized

I understand why the compiler is warning me, but the very purpose of my code is to handle situations like this: when variables are not initialized, I close my program. In that case, I never use them.

I think the answer is pretty simple: initialize "begin" and "end" outside of a "try" block, for example by assigning zeros to them right away.

int begin = 0, end = 0;

      

But can I force the compiler to just ignore the problem? Is there a way to disable such curious checks in my code? This doesn't seem like a technical problem with my program, outside of possibly wrong values ​​for my variables.

I am using IntelliJ IDEA Community Edition 14.1.2

+3


source to share


4 answers


From the javadoc System.exit

:

Shuts down the running Java virtual machine.

Your compiler doesn't look ahead to figure out that jvm will close and the program will stop working.



If you change System.exit(1);

to return;

, the compiler knows that at that point your request will return control flow to the calling method. Since you are in a main

class method of main

your program, it will also terminate the program execution. Only now does your compiler know about it.

You can use the following code, but remember that in this case, you will not ship status code 1

to your operating system:

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

      

0


source


Not. You cannot turn it off. The compiler insists that you don't use uninitialized variables. This is a Java rule.



You need to fix your code.

+4


source


Like other users, you do not disable this error, you are fixing a bug in your code. I offer this job

public static void main( String[  ] args ){
    int begin, end;
    try{
        begin = Integer.valueOf( args[ 1 ] );
        end = Integer.valueOf( args[ 2 ] );

        if( begin >= end ){
            System.out.println( "Wrong arguments. (" + begin + " >= " + end + ")" );
            System.exit(1);
        }
        System.out.print( "OK." );
        System.exit(0);
    }catch( NumberFormatException conversion_error ){
        System.out.println( "Not A Number." );
        System.exit( 1 );
    }
}

      

0


source


There are maybe legitimate reasons why you can ignore / suppress a variable that cannot be initialized with a warning ... for example when you initialize a variable in the @Before method in a test. In this case, you can use:@SuppressWarnings("InstanceVariableMayNotBeInitialized")

Note. I would add the end of a comment explaining why

@SuppressWarnings("InstanceVariableMayNotBeInitialized") // Initialized in @Before

0


source







All Articles