+ 1

Technical Support Executive

This code 2 and 3 SOP lines are uncorrected. Why is it? import java.util.*; class Example{ public static void main(String args[]){ final int x=100; final int y; y=100; int z=100; int a,b,c,d,e,f,g; if(x>0){ a=1; } if(y>0){ b=1; } if(z>0){ c=1; } if(z>0){ d=1; }else{ d=2; } if(10>9){ e=1; } if(true){ f=1; } g=z>0 ? -1:1; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(f); System.out.println(g); } }

27th Jun 2020, 2:46 AM
Buddika Abeykoon
Buddika Abeykoon - avatar
3 Answers
+ 3
final int y; y = 100; A blank final variable should not be declared and then set on a different line like this. Blank final variables can be initialized in a constructor or instance intializer block. While this doesn't cause an immediate error it is contributing to your errors as the compiler knows the value is being set, but unlike the declaration correctly done prior to it, it doesn't know its value and therefore cannot do a compile time replacement of the value. This is why you're getting the error for the variable b. For the if block where the variable c is set, you can either also make z final or initialize the variable c in the declaration. final int x=100; final int y=100; final int z=100; This takes care of the errors, because the compiler can do a direct replacement of the variables values and evaluate the if blocks at compile time to see the other values are set. This is also, why there isn't any errors pertaining to the variables e and f.
27th Jun 2020, 4:57 AM
ChaoticDawg - avatar
Technical Support Executive? What is the relation between technical support executive and the code problem?
27th Jun 2020, 3:32 AM
Buddika Abeykoon That is not output. That is error. You should initialize variables with 0.
27th Jun 2020, 4:30 AM
A͢J - avatar