What is the difference between static variables and instance variables?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between static variables and instance variables??

Why static variables are called class variable?. Why non static variables are stored in object memory? https://code.sololearn.com/c0Szxm9cnD11/?ref=app

7th Feb 2019, 8:17 PM
Aazad Waf 🌀
Aazad Waf 🌀 - avatar
4 Answers
+ 4
Expanding on peter's answer... Static members are bound to the class type which exists once per process. Instance members are bound to an object instance of a class type and apply to their respective object instances.
7th Feb 2019, 9:40 PM
David Carroll
David Carroll - avatar
+ 3
We do not need to create object to use static members ( fields or methods ). For example, Its useful when we need to count how many instances of certain class was made.
13th Feb 2019, 8:14 PM
Lev Tolstoy SPB
Lev Tolstoy SPB - avatar
+ 1
one belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class. class variables well these are also known as static member variables and there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.
7th Feb 2019, 8:43 PM
peter
peter - avatar
0
It's about scope and efficient memory management. Static variables can be accessed anywhere independent of any object.
7th Feb 2019, 8:42 PM
Division by Zero