What is static ? can anyone elaborate me with an example? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is static ? can anyone elaborate me with an example?

5th Sep 2016, 5:45 AM
Vishvapalsinh Paramar
Vishvapalsinh Paramar - avatar
1 Answer
+ 1
When you create an object of any class, it is said you create an instance. Each instance has a number of variables that belongs to that instance. So, if you create two instances of the same object, each of those will have the same variables but can have different values. Instance A will have a variable called VAR with a value of 7. An instance B will have a variable called VAR with the value of 9. They can have different values since they are INSTANCE variables. Static variables don't belong to the instances but to the class itself instead. So, two instances of the same class will have access to write or read that variable but the static variable will have only one value shared between all the instances of the class. If instance A makes static VAR = 5, if you try to read the value of static VAR from any instance, it will return 5. It a class variable, not an instance one. Hope I've explained myself.
6th Sep 2016, 5:11 PM
Oleiros S.L.
Oleiros S.L. - avatar