0

hi... i probably didn't understand something but the following code doesn't lead to an error.. it changes the value of PI and prints it.

class MyClass { public static final double PI = 3.14; public static void main(String[ ] args) { System.out.println(PI); double PI = 3.15; System.out.println(PI); } }

23rd Feb 2016, 7:56 AM
Orpheus
Orpheus - avatar
2 Answers
+ 2
That is because the PI variable that you created inside main method has scope only inside rhe main method. It is totally different from the one you created before the main method. The PI that you initialized as final belongs to a different scope. Please review the concept of scopes and life span of variables.
24th Feb 2016, 12:44 PM
Srijal Joshi
Srijal Joshi - avatar
0
you are declaring a new PI variable inside main. so in this case, no error, since the program will not use the constant, but the new declared value inside main
24th Feb 2016, 6:33 PM
Kelvyn Abreu
Kelvyn Abreu - avatar