class C{
 public static void main(String a[])      {
  int i1=9;
  int i2;
    if(i1>3) {         
        i2=8;
     }
   System.out.println(i2);
}}

Edited 3 Years Ago by Nick Evan: Fixed formatting

What do YOU think is wrong with it? Have you compiled it? Have you executed it? Have you tried executing it with a different value for i1 (eg 0)?

You are trying to print out a variable(i2) which is possibly uninitialized (depends on rutime, i2 won't be initialized if i1 < 3). Either provide an initial value for i2 or write an else statement which takes care of the same.

Hey....
while declaring any arguments in main()
you have to initialse that variable with any value..
Otherwise you have to face the ERROR called ---- VARIABLE MIGHT NOT HAVE BEEN INITIALISED...

the error is a compile time error.The error is:
VARIABLE MIGHT NOT HAVE BEEN INITIALISED

But after declaring the variable 'i as final' the code runs just fine. Y??

You are trying to print out a variable(i2) which is possibly uninitialized (depends on rutime, i2 won't be initialized if i1 < 3). Either provide an initial value for i2 or write an else statement which takes care of the same.

The condition is i1>3. And after declaring i1 as final the code runs just fine .Y??Why if i dont declare i1 as final there is an error but after declaring i1 final it compiles fine.

after declaring the variable 'i as final' the code runs just fine

Your code doesn't show a variable: 'i'?

the error is a compile time error.The error is:
VARIABLE MIGHT NOT HAVE BEEN INITIALISED

But after declaring the variable 'i as final' the code runs just fine. Y??

Because when you declare a variable as `final`, the compiler ensures that re-assignment to that variables never happens. Using static program analysis, the compiler can infer that the value of i1 will *always* be 9 in the given scope and can in turn infer that i2 will always be set to 8. In fact, a smart compiler might reduce the code to something like:

final int i1 = 9;
int i2 = 8;

// OR even this if i1 is never used again in the scope
int i2 = 8;

Edited 6 Years Ago by ~s.o.s~: n/a

Because when you declare a variable as `final`, the compiler ensures that re-assignment to that variables never happens. Using static program analysis, the compiler can infer that the value of i1 will *always* be 9 in the given scope and can in turn infer that i2 will always be set to 8. In fact, a smart compiler might reduce the code to something like:

final int i1 = 9;
int i2 = 8;

// OR even this if i1 is never used again in the scope
int i2 = 8;

But i1 has been declared as 9 in first line of the code(i1=9).Why the code is not able to initialize the value of i2 as 8.The condition comes to be true in the if block.Can u explain in detail.M a bit confused here.

If i1 is not declared `final`, the compiler has no way of knowing in advance that the value of i1 won't change; hence it complains if you don't provide an ELSE clause. When i1 is declared as `final`, the compiler knows for sure that i1 will *always* be 9 and never change hence it stops complaining.

Even if you know for sure that i1 never changes before the IF check, the compiler is not *that* smart.

No, I meant i1 since it is the variable which is initialized. Removing or adding the `final` quantifier to i1 changes the way the compiler treats the presented source code. If i1 is made final, the uninitialized state of i2 is acceptable since it is anyways going to get initialized in teh IF block. If not, the user is presented with a compile time error.

In other words, the compiler recognizes that i1 is > 3 and reduces that if condition to true

Your code doesn't show a variable: 'i'?

hey norm ..the variable is i1..and the program runs jst fine if i declare i1 as final..i want to knw y..why the code runs fine if i1 is declared final ??please be a bit detailed..if i dont declare it final the code isnt going into 'if' block.y??

Aditya:
The compiler is required to ensure that variables are not unassigned when they are referenced. Since the assignment of i2 occurs within an "if" (and not anywhere else) the compiler assumes that (i1>3) might not always be true, and therefore i2 might sometimes be unassigned. When you declare i1 to be "final", this allows the compiler to treat it as a constant - everywhere it sees "i1", in the compiled version you'll see "9". (9>3) is always true, so the compiler will let you get away with this.

You could also put an else { i2 = 12 } and the compiler would be happy, since both branches end up with i2 being assigned.

Ok, listen, even though i1 = 9 on the first line, it is not FINAL. Java will not recognize this, and in it "mind" (bad work here), I should say in the JVM, it recognizes this as a potential source of error. In a regular program, non-final variables are changed most of the time, hence the word VARIABLE. IF it is not final, the JVM will think that the variable is subject to change during runtime, which it technically is. So, because i2 is not initialized, it forces you to initialize it. Because you made i1 final, it was recognized as always being greater than 3, then the statement is always true and it will compile properly. Is that clear enough? :D

Ok, listen, even though i1 = 9 on the first line, it is not FINAL. Java will not recognize this, and in it "mind" (bad work here), I should say in the JVM, it recognizes this as a potential source of error. In a regular program, non-final variables are changed most of the time, hence the word VARIABLE. IF it is not final, the JVM will think that the variable is subject to change during runtime, which it technically is. So, because i2 is not initialized, it forces you to initialize it. Because you made i1 final, it was recognized as always being greater than 3, then the statement is always true and it will compile properly. Is that clear enough? :D

Thanks..i get it.:-)

This question has already been answered. Start a new discussion instead.