0

How to change the value of static variable?

7th Feb 2017, 6:53 AM
Ashish Wayne
Ashish Wayne - avatar
2 Answers
+ 1
Static variables are accessed in the same way all other variables are accessed depending on their access modifiers (private, public, protected, default/package). If it is made final, however it can't be changed.
7th Feb 2017, 7:42 AM
ChaoticDawg
ChaoticDawg - avatar
0
The static variables have a single copy for the entire class and you can access and change the static variable using the class name. Ex. class Aclass { static int num = 10; public void method(){ num = 20; } } class Bclass { Aclass.num = 30; } We don't need to create an object of Aclass to access the static variable num, we can use class name for accessing the variable.
7th Feb 2017, 7:01 AM
Varun Moghe
Varun Moghe - avatar