I have the following code:
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);
}
When I try to compile it, I get this error.
Error:(13, 13) java: variable begin might not have been initialized Error:(13, 22) java: variable end might not have been initialized
I understand why compiler is warning me, but the very purpose of my code is handling such situations: when variables are not initialized I shut down my program. In such case I never use them.
I think that answer is fairly simple: initialize "begin" and "end" outside "try" block, for example by immediately assigning zeros to them.
int begin = 0, end = 0;
But can I force compiler to just ignore the issue? Is there any way to switch off such inquisitive checks of my code? It's not like there is any technical problem with my program, outside of possibly wrong values of my variables.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With