Why cant i change variable values of instance variable while we can change value of local variables of same variable in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why cant i change variable values of instance variable while we can change value of local variables of same variable in java?

class example{ int a=2; a=4; System.out.println(a); } output error<identifier> why?

12th Nov 2018, 9:18 AM
Nikki Singh
Nikki Singh - avatar
2 Answers
+ 1
Its not like you cant, but those are reserved for defining your class instance variable, and method. Btw if you want to do such an operation outside any method you can use anonymous block, simply {} without name class Bar{ int a=2; { a=3; } }
12th Nov 2018, 9:39 AM
Taste
Taste - avatar
+ 1
thanks your answer is "tasty".😋
13th Nov 2018, 10:36 AM
Nikki Singh
Nikki Singh - avatar