How a static keyword work?? Anyone | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How a static keyword work?? Anyone

How it will work..? how memory allocation will be for static class and members??

28th Apr 2017, 3:58 PM
tharun
tharun - avatar
1 Answer
+ 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
28th Apr 2017, 4:37 PM
seamiki
seamiki - avatar