0
How to change the value of static variable?
2 ответов
+ 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.
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.