+ 1
How a static keyword work?? Anyone
How it will work..? how memory allocation will be for static class and members??
1 Resposta
+ 4
let's say you have a static variable in a class.
you create (instantiate) multiple objects from the mentioned class.
if one object changes the value of the static variable, all other objects from the same class will be affected by the change.
let's say you have a class of circles
with a static variable called diameter that equals 3
you create 3 objects from this class
firstObject.diameter will be 3
secondObject.diameter will be 3
thirdObject.diameter will be 3
let's say in your code the second object changes the value of diameter to 38:
secondObject.diameter=38
now
firstObject.diameter will be 38
secondObject.diameter will be 38
thirdObject.diameter will be 38
IF the diameter variable was NOT DECLARED STATIC the change would have been limited to the object that made it:
firstObject.diameter would have been 3
secondObject.diameter would have been 38
thirdObject.diameter would have been 3