Instance variables(variable that is declared inside the class but outside the methods) are initialized by default(in this case to false). Thats whey your first example works. However local variables(the variable that u mentioned inside your main method) are not initialized by default. Hence you get a compilation error.

Jessica Lang

Ranch Hand

Posts: 61

posted 14 years ago

Hi Ram, Thanks for the explanation. I tested the codes....initialising TestVariableInit2.java variable to false and it compiled perfectly. Then, I initialised the boolean variable in VariableInit.java to true. When I ran the TestVariableInit, it gave me a value of true (due to my earlier initilisation). By playing with the codes, it helps me to understand the "Java behaviour" better..... Thanks again.....