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); } }
2 Respuestas
+ 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.
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