THis is a simple question, but the answer is kinda complicated.
When you try to compile the first version:
class A{
int a;
};
int A::a=10;
Youi will get an error message telling you that "
a
is not a static member of class
A
" and that is absolutely accurate - but unless you understand classes, instances, and static variables / methods it's not really helpful.
There are two types of "elements" that a class can have: static elements, and instance elements (where an element is a field, property, method, event, or delegate)
A static element is shared by all instances, and is accessed via the class name.
An instance element is unique to each different instance of the class and is accessed via the variable holding the instance reference. Which doesn't seem to make more sense either - but it does and you are very, very aware of this in the real world!
Think about cars for a moment: all cars have a colour - but which colour it is depends on which specific car you are talking about. My car is black; your car is red; this car is green; that car is blue. Colour is an instance property of the Car class because you need to have a specific instance of a Car in order to ask the question "what colour is it?" - you can't say "what colour is a car?" because it's meaningless without saying which car you mean.
But cars have static properties as well: you can ask "how many wheels has a car?" because all cars have four wheels. (If it had two, it would be a motorbike, not a car)
What your first example is trying to do is "set the colour of all cars" which is impossible; your second is trying to "set the colour of this specific car" which is very possible!
Make sense so far?