In java, how to initialize final parameters after a loop that can or not initialize them

I use java. I have two final parameters.

final String para1;
final String para2;

I have a loop to initialize them, for example:

for(int i = 0; i<array.size(); i++){
 if(i==array.get(i)){
  para1 = something;
  para2 = something;
 }
}

but intellij will say that: para1 and para2 may not have been initialized. How to change the code? I have to save para1 and para2 final, and I want to assign them null if they are not initialized

+4
source share
1 answer

Just use two other temporary variables:

int t1 = 0;
int t2 = 0;
for (int i = 0; i < array.size(); i++) {
    if (i == array.get(i)) {
        t1 = something;
        t2 = something;
    }
}
para1 = t1;
para2 = t2;
+5
source

Source: https://habr.com/ru/post/1598928/


All Articles