Take the 2-minute tour ×

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.

share|improve this question

marked as duplicate by Basile Starynkevitch, Eelke, Dietmar Kühl, Donal Fellows, PearsonArtPhoto Nov 23 '12 at 9:56


If all the similar questions and answers don't fit you, please read a good C++ programming book, look at the generated assembly code, or switch to some other language (e.g. Ocaml). A non-programmer won't care. A programmer should know that each instance of class has its own non-static member variables, while there is only one static member variable shared by all instances. –  Basile Starynkevitch Nov 23 '12 at 6:29
They're not identical questions — the x-ref'd question is about global variables not within a class. There's similarity, sure — but definitely not exact duplicate. –  Jonathan Leffler Nov 23 '12 at 6:30
@BasileStarynkevitch not a duplicate of that one. Firstly, that question is about C, not C++. Secondly, the static keyword as an additional meaning in C++. –  user529758 Nov 23 '12 at 6:32
@BasileStarynkevitch These questions are very VERY different, trying to understand the static keyword from those questions (including the one pointed to by the OP) to apply it to the class-scope meaning would be nothing but pure confusion. I think posing them as "similar questions" could be confusing to novice C++ programmers coming here. –  enobayram Nov 23 '12 at 6:39

4 Answers 4

up vote 5 down vote accepted

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 own int i variable 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 the static int i, but none of them will own it (it's shared).

share|improve this answer
+1 I like this analogy. –  Mysticial Nov 23 '12 at 6:31
i really get this analogy thanks man –  theuserkaps Nov 23 '12 at 6:36
+1 for the beginner friendly explanation, but I think one should be careful with this analogy, the organization-worker relationship is not an exact fit for the class-instance relationship. –  enobayram Nov 23 '12 at 6:43

Static inside a class means that all instances of the object: B a,b,c,d; have the exactly same member i. a.i++; changes the contents of b.i, c.i etc.

The difference of static inside a class and outside of it is to make the static variable visible only to or through the class. Of course the variable is not stored in the class but somewhere else. This syntax just allows defining the scope of the variable to this particular class.

And of course a non static member is unique to each instance.

share|improve this answer
+1 because this actually answers the question. –  user529758 Nov 23 '12 at 6:32

If you declare multiple objects of B then every object will have its own copy of int i. However there will be just one static int i for all the objects.

B::i //This will refer to the static i
//As you can see this is accessed through the class name

B obj, obj2;
obj.i //This will refer to the obj member i 
obj2.i // and will be different from this which is obj2 member i
share|improve this answer

To avoid confusion of duplicate names, imagine the following:

class foo
       int i;
       static int j;

static int j; defines a single variable, called j that is associated with the class foo. No matter how many objects of type foo you create, there will only ever be one j. In this case anything outside the class foo can access j by using the syntax foo::j.

Contrast that to the definition of i, which is a member of an object of type foo. There is a unique i created for every object of type foo. eg.

foo myObject;
foo myOtherObject;

myObject.i = 42; myOtherObject.i = 180;

share|improve this answer
