can a defined variable be defined again? | Sololearn: Learn to code for FREE!
+ 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[])"?

15th Oct 2017, 2:39 PM
YuHai
YuHai - avatar
2 ответов
+ 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
15th Oct 2017, 2:47 PM
Zephyr Koo
Zephyr Koo - avatar
+ 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).
15th Oct 2017, 4:35 PM
Rrestoring faith
Rrestoring faith - avatar