x


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?

more ▼

asked Apr 05 '12 at 02:58 PM

Dreoh gravatar image

Dreoh
49 6 10 12

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

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.

more ▼

answered Apr 05 '12 at 03:39 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thank you!

Apr 05 '12 at 04:00 PM Dreoh
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 05 '12 at 03:01 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

Thank you!

Apr 05 '12 at 04:00 PM Dreoh
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 05 '12 at 03:19 PM

fafase gravatar image

fafase
10.5k 9 15 40

This is true, but don't mix up scope and extent

Apr 05 '12 at 03:31 PM Bunny83

Thank you!

Apr 05 '12 at 04:00 PM Dreoh
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5058
x3318
x276

asked: Apr 05 '12 at 02:58 PM

Seen: 578 times

Last Updated: Apr 05 '12 at 04:00 PM