Why it's is essential variable(b) to be static? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why it's is essential variable(b) to be static?

class A { static int b;//without static error public: void show() { cout <<b; } }; int A :: b; int main() { A x; x.show(); } (output 0)//how it's 0

12th Aug 2018, 1:00 PM
Coder21
Coder21 - avatar
4 Answers
+ 5
Coder18 It being essential depends on what the code needs to accomplish. The static value remains the same for each instance of the class, it is defined inside of.
12th Aug 2018, 1:13 PM
Manual
Manual - avatar
+ 2
Coder18 edit: in your example you did not give b a value making it 0. https://code.sololearn.com/cB4rCf31BS9l/?ref=app
12th Aug 2018, 2:13 PM
Manual
Manual - avatar
+ 1
#Manual if not write static in this code,there is an error,why?
12th Aug 2018, 2:02 PM
Coder21
Coder21 - avatar
0
and this code output 0,so b=0;but there isn't adjudged 0 to b anywhere :(
12th Aug 2018, 2:08 PM
Coder21
Coder21 - avatar