- Home /
What exactly is static?
I know its not necessarily a Unity question, but I've googled a bit what static is, but can't find an explanation that I can easily comprehend..
anyone got an explanation on static variables in layman's terms?
Answer by Statement · Apr 05, 2012 at 03:39 PM
In laymans terms:
There exists only one copy of a variable if it's static (accessed through the class type).
There can be many copies of a variable if it's not static (accessed through an object reference).
Static variables are said to belong to the "class" and non-static (member variables) are said to be long to an "object" (an instance of a class type). Since there is only one class, there is only one static variable. Since there can be many objects of a class, there can be many member variables.
When I said there exist only one copy of the static variable, I did not mean to say that the value it holds is unique, it's only the symbol that is unique.
There are special cases to static variables where they can be unique per thread, but the default behaviour is unique per assembly.
Answer by fafase · Apr 05, 2012 at 03:19 PM
Static means that the variable belongs to the class and not the object (in Unity).
That means that only one instance of it will be created and will remain over any scope.
For example, your enemy class would have a static enemyCount; This variable will be the same for all enemies as there will be only one instance of it. Shall you destroy an enemy object or all of them, the variable is still there, if you create a new object then he will access that same variable.
Answer by Kryptos · Apr 05, 2012 at 03:01 PM
Wikipedia: Static variable
MSDN: Static Classes and Static Class Members (C# Programming Guide)
A static method, field, property, or event is callable on a class even when no instance of the class has been created.
In other words, static properties or methods are shared by all instance of a class. Whereas member properties and methods belongs to a particular instance of the class.
Your answer
Follow this Question
Related Questions
GetInstanceID ?????? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Accessing another var on another script 1 Answer
Play audio on keyboard click? 1 Answer
How to emit particle from a collision? 2 Answers