Hi All, I have a problem.According to the rules an assignment to the local variable must occur on every possible execution path to the access.But why I'm getting an error here. class assign{ void status(int a){ String s; if ( a>=0 ) { s="A Positive Number"; System.out.println(s);} if ( a<0 ) { s="A Negative Number";System.out.println(s);}<br /> }<br /> and why does it compile??<br /> class assign{<br /> void status(int a){<br /> String s;<br /> if ( a>=0 ) { s="A Positive Number"; System.out.println(s);} else { s="A Negative Number";System.out.println(s);} } ThanX
public static void main(String args[]) { assign ob=new assign(); ob.status(-5); } } Well, the code above matches your first code. (It is just a bit formatted for clear readability, and I have added a main() method to execute it.) It did compile correctly at my system and even ran perfectly. The second code also compiled correctly and ran. Choosing a number less than 0 prints "A Negative Number" on the shell prompt. Where is the problem, (if, I missed it!!!). Bye, Tualha Khan
Hina, It does compile. Perhaps you should try adding another } at the end ? Rgds Sahir
....
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4095
1
posted
0
I seem to remember reading about this and it wasnt supposed to compile. well it gives me something to look into tomorrow.
Hina Mustafa
Greenhorn
Joined: Dec 01, 2000
Posts: 8
posted
0
Hi, Sorry I missed final with String s; I mean 3rd line is final String s; Now there's an error cannot assign a second value to blank final variable I'm also confused with another variation class assin{ void method(int a){ String s; if ( a>=0 ) { s="A Positive Number";}//1 if(a<0) { s="A Negative Number";}//2 System.out.println(s); } } with this code either line 1 or 2 will excecute so there should not be error according to the rules..
[This message has been edited by Hina Mustafa (edited December 11, 2000).]
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4095
1
posted
0
Indeed Tualha's code does compile and run printing "A Negative Number". the JLS says in chapter 16 that it should not.
Except for the special treatment of the conditional boolean operators &&, | |, and ? : and of boolean-valued constant expressions, the values of expressions are not taken into account in the flow analysis. As another example, a Java compiler will accept the code:
void flow(boolean flag) { int k; if (flag) k = 3; else k = 4; System.out.println(k); } as far as definite assignment of k is concerned, because the rules outlined in this section allow it to tell that k is assigned no matter whether the flag is true or false. But the rules do not accept the variation: void flow(boolean flag) { int k; if (flag) k = 3; if (!flag) k = 4; System.out.println(k);// k is not "definitely assigned" before here } and so compiling this program must cause a compile-time error to occur.
Hi All, Think I figured it out If you move the <code>System.out.println(s)</code> outside the <code>if</code> blocks you get the <code>s might not have been initialized</code> error.
Since, in the original code, 's' is only referenced after an assignment has been made the compiler knows it will always been initialized. Isn't Java fun ! ------------------ Jane The cure for boredom is curiosity. There is no cure for curiosity. -- Dorothy Parker [This message has been edited by Jane Griscti (edited December 11, 2000).]
thanks so much Jane. this one had me going. it seems so obvious now. i didnt really think the JLS would contain an error of that magnitude. i certainly would have gotten it wrong if it had been on a test.