SCPJ2
"The enemy is never the enemy in his own eyes." - Sun Tzu
Originally posted by swapna g:
Hi ..
I couldnt understand the difference in these 2 codes---that how come the first doesnot raise any
compiler error but the second one does???
void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}
The above code is ok.
void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
if (!flag) {
k = 4; // k is not "definitely unassigned" before here
System.out.println(k);
}
}
But i feel both are same as for compiler
both ensure the definite assignment of the
local var k.
![]()
swapna
Originally posted by James Childers:
*You* are smart enough to tell that only one of these if statements will be executed, but the compiler is not so smart.[/QB]
Originally posted by swapna g:
Hi ..
I couldnt understand the difference in these 2 codes---that how come the first doesnot raise any
compiler error but the second one does???
void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}
The above code is ok.
void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
if (!flag) {
k = 4; // k is not "definitely unassigned" before here
System.out.println(k);
}
}
But i feel both are same as for compiler
both ensure the definite assignment of the
local var k.
![]()
swapna
Christian
SCPJ2
Originally posted by swapna g:
Thank u everyone...i got it!
I strongly agree with corey ,roy and Amish abt the if/else construct..!!!
Actually the problem is not with the variable
being declared as final but because of
if/else construct..as i tried the code
even removing the final modifier and still the
same result i got!!!
![]()
swapna:
Are you sure about removing final still working?
I tried this code too, it works if I remove final modifier. final modifier sure plays a rule in this error, since you cannot assign twice to a
final variable, while you can change a non-final
variable's value more than once.
victor
Originally posted by swapna g:
void unflow(boolean flag) {
int k;
if (flag) {
k = 3;
System.out.println(k);
}
if (!flag) {
k = 4; // k is not "definitely unassigned" before here
}
System.out.println(k); //wont compile!!!
}
SCPJ2
permaculture is giving a gift to your future self. After reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|