2

I do not have a background on C/C++. I just started learning Objective-C after a past using other languages.

In which situations should I use static declaration of a variable over regular ivars or properties? What do I gain doing this?

thanks

Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53
Duck
  • 34,902
  • 47
  • 248
  • 470
  • possible duplicate of [How SHOULD you make (and use) static libraries on the iPhone](http://stackoverflow.com/questions/1563623/how-should-you-make-and-use-static-libraries-on-the-iphone) – karthikr Jun 18 '13 at 01:48
  • 2
    I am not talking about libraries. I am talking about variables. Please remove this banner from my question. Thanks. – Duck Jun 18 '13 at 01:52
  • why not tag an objective-c question with the objective-c tag? – vikingosegundo Jun 18 '13 at 01:59

5 Answers5

5

Global variables and functions

By default, all symbols (global variables and functions) are exported (made visible to code in other source files). If a global variable is declared static, it is not exported. That means it is only accessible to code in the current source file.

This is useful when you have a global variable that you want to restrict access to, and don't want to worry about a name collision. For example, if you wanted to maintain a counter to track how many instances of a class have been created, you could create a static int gInstanceCount. Since it is static, you would know that (1) no other code can modify the variable and (2) if some other file uses a global with the same name, there won't be any collisions.

Static declarations in header files

Note that when you put something in a header file, it is as if you copied-and-pasted that code into every other file that includes it. That means if you declare something as static in a header file, every file that includes it gets its own copy of that thing.

That means if you declare static int foo in Foo.h and then write execute foo = 4 in Bar.m, when you try to access that value in Other.m you won't necessarily get 4 back.

Local static variables

You can also define a local variable (inside a function or method body) as static. Normally, local variables are allocated on the "stack" which means they are created when your function is executed and deallocated when the function exits. If two threads enter the same function at the same time (or one thread makes a recursive call back into a function) each thread gets a fresh chunk of memory to work with, and anything it does won't affect any other thread.

However, a local static variable is stored on the "heap" instead. All executions of the function share that same memory location. Also, when the function ends, the value stays where it is. That's why a local static variable is often used in sharedInstance methods on Objective-C singleton objects.

In most ways, a local static variable basically acts like a global variable that can only be seen inside the function which declared it.

benzado
  • 82,288
  • 22
  • 110
  • 138
  • There is a difference between the concept of visibility and linkage. Visibility (scope) is defined in ISO/IEC 9899:TC3, section 6.2.1, linkage is defined in ISO/IEC 9899:TC3, 6.2.2 – Amin Negm-Awad Jun 18 '13 at 21:35
  • @AminNegm-Awad That's interesting! I probably am being a little loose with my terms but I'm trying to make a useful answer as well as an accurate one. Does the difference invalidate anything I wrote above? I'll edit it if necessary. – benzado Jun 18 '13 at 22:20
  • Yes: "(made visible to code in other source files)" No variable in a translation unit is visible in another translation unit. It is visible, if it is declared in a translation unit. Then, it is linked. There is no export. Using statics in a Header seems to have no sense. Why should Foo.h create a variable in every translation unit? Foo.m cannot access it. – Amin Negm-Awad Jun 19 '13 at 05:34
  • @AminNegm-Awad Yes, a static in a header file is usually a mistake. That's why I wanted to call attention to it. Regarding my use of "export": it seems to me an acceptable abstraction for this question, without getting into a lot of detail about the compiling and linking process. – benzado Jun 19 '13 at 18:29
1

the static keyword is used to provide scope to global variables. Normally, global variables defined outside of a function have a public scope, and are visible to all .m or .c files in a project. making a variable static allows you to have a "global" variable which is scoped (visible) to only the .m or .c file containing the variable definition. This allows you to have a variable which can be shared by multiple functions in a single source file, while avoiding potential naming conflicts. Also of note, the extern keyword, which allows you to indicate that a specific global variable is initialized in a different source file but used in this source file.

Claies
  • 22,124
  • 4
  • 53
  • 77
1

The keyword static is overused in C. It means several different things. In some contexts, it just means that field is only understood in the remainder of that file (compilation unit). In other words, it can't be linked to from some other file.

In Java and C++, static class members are defined at the class level, not the individual object level, so the one value is shared by all objects of that class (or one of its subclasses). Unfortunately, IMHO, Objective C does not support this. Instead you use statics at the file level.

Community
  • 1
  • 1
Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53
0

If you don't know if you need a static variable, you probably don't need one.

One reason you might use a static variable is to provide a class variable (that is, one variable for an entire class, rather than an instance variable, which each instance of a class would have it's own copy of).

This question (and its accepted answer) explain how one would use static variables to simulate a class variable in Objective-C.

Community
  • 1
  • 1
zpasternack
  • 17,838
  • 2
  • 63
  • 81
  • static has nothing to do with scope. – Amin Negm-Awad Jun 18 '13 at 21:50
  • While `static` does not technically have to do with scope, it does have to do with _linkage_ (for file-level static variables and functions) which can have much the same effect. – Andrew Lazarus Jun 18 '13 at 23:16
  • No. Scope describes the effect of having the possibility to use an identifier at a location in source. A ("global") identifier is usable in an translation unit, if it is declared and only if it is declared. This does not depend on, whether it is declared with external linkage, internal linkage oder no linkage. Linkage describes the effect, whether *if* a ("global") variable is declared twice in different translation units are out together. – Amin Negm-Awad Jun 19 '13 at 05:41
0

First: "object" in this context refer to "c objects", in very easy words, something, that lives at runtime. It has nothing to do with objects in the sense of OOP or Objective-C. You can think of an object as a var.

static has nothing to do with scope. static on an extern identifier (= declared outside a block) has internal linkage. The results of linkage are defined in ISO/IEC 9899:TC3, section 6.2.2. Scope is defined in section 6.2.1. Internal linkage means, in short words, that two identifier in different translation units (".m files") do not denote the same object, but different objects. Every translation unit has its "own" object.

If an identifier is not extern (= inside a block) declared with static storage class it has a lifetime from begin of the program execution until the program stops running.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50