Possible Duplicate:
What is the difference between static int a and int a?
I’m trying to get just a simple explanation in simple English. I have tried to read and research on the difference between static int and just int but whatever I read it seems to confuse me more. Let’s say I have a class like this below
class B{
static int i;
int i;
...
}
In simple English, what is the difference between the two? Imagine you are explaining to a non-programmer.
Someone asked a similar question like this here but it’s not to my satisfaction.
Here’s a metaphor for you.
Imagine an organization that has a bank account. That organization, in terms of C++, is your
class. Imagine that the organization has representatives (workers, if you want). They are, in terms of C++, instances (or variables) of that class.Now, each representative can have his own bank account that is available only to him. No other guys can use it. That would be a normal
int.However, each worker can also use the organization’s bank account which is shared by them all and doesn’t belong to any of them in particular. That is your
static int.Now, back to technical terms again, in case you will want it. I’ll try to be clear and concise.
Normal
int:Each variable of the class type will have it’s own personal variable. So in your example, if you make 10 variables of the type
B, each and every of them will have their ownint ivariable inside.static int:Static variables are kind of shared by all the variables of the class. A static variable doesn’t belong to any of the class variables, it belongs to the class itself. So if you will create 10 variables of the type
B, each of them will be able to access thestatic int i, but none of them will own it (it’s shared).