Reputation: 277
I have 2 variables, and according to some case in an if else I either use the code with the first variable variable1 or else the code with the second one variable2.
Both of the variables are declared as final and I only assign them at the condition and I use different statements on them.
Any idea on how I could disable the android warning for both of these statements ?
Is there any other way to do this without using asserts or checking if the variable has the value null after I initialize it in the if or else ?
I am very certain that my code does work and there is no way I use a variable which is not initialized, I would prefer a way to disable only the warnings for these statements and not for the whole project.
Which would be the best way to implement this ?
private static class foo {
private final objectwhatever variable1;
private final objectwhatever variable2;
public foo(){
if ( condition 1)
{
variable1 = new objectwhatever();
statements in variable1
}
else ( condition 2)
{
variable2 = new objectwhatever();
statements in variable2
}
}
}
Upvotes: 2
Views: 5576
Reputation: 105
If you must suppress it, use @SuppressWarnings("InstanceVariableMayNotBeInitialized")
, suppressing "ALL" is kind-of overkill.
Upvotes: 1
Reputation: 15165
A final
variable must be assigned once in the constructor or out of it. This is a Java rule, you cannot disable the error warning. But, if you remove final
, the error will disappear.
For some reason, you may want to disable all Android lints, i.e. by using @SuppressWarnings("ALL")
to your class or method:
@SuppressWarnings("ALL")
private static class foo {
private objectwhatever variable1;
private objectwhatever variable2;
...
}
Hope that helps!
Upvotes: 1