Local variable in java
//All code is in java Language.
public class TestLocal
{
public static void main(String [] args)
{
int x;
if (args[0] != null)
{ // assume you know this will
// always be true
x = 7; // statement will run
}
int y = x; // the compiler will choke here
}
} So,My Question why compiler choke here.Does it bypass if statement(This
look quite horrible.)..If I initialize x outside if statement block then
compiler do not complain..I mean this code:
public class TestLocal
{
public static void main(String [] args)
{
int x;
x = 7; // compiler is happy now..:)
if (args[0] != null)
{ // assume you know this will
// always be true
// statement will run
}
int y = x; // the compiler not complain here now.
}
}
Why such a horrible behaviour of complier?
No comments:
Post a Comment