+ 2
can a defined variable be defined againïŒ
int num2 = 4; int num3 = ++num2; int num3 = --num2; why it tells me" variable num3 is already defined in method main(String[])"ïŒ
2 Answers
+ 16
You cannot redefine or declare another variable with the same identifier or name within the same scope. However, you can *reassign* it with a new value by removing the data type in front. đ
P/S: Updated scoping and credits go to @Rrestoring faith
+ 4
You can have two variables of the same name if one is global and the other is local. Though, that isnt re-defining a variable. Since the compiler can determine which one you are referring to (local variables have higher priority).