2

At the top of my .m files I traditionally have a const int varWidth = 42; or something similar. If I want it public I'll add extern const int varWidth in my .h.

However, after reading this link it's recommended apparently that static is prepended if it's used solely in the .m file (but not if public). Why is this?

I understand static means it will only be initialized once and its value will persist for the full execution of the program, but I don't see how that's important if it's a const anyway.

Community
  • 1
  • 1
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 1
    `static` at function-level scope becomes a lifetime qualifier. `static` at top-level scope indicates internal linkage. – CodaFi Apr 14 '14 at 20:21

3 Answers3

2

In C and relative languages (Objective C is one of them), the static storage class specifier is used primarily for information hiding.

When you prepend the static keyword to a variable declaration, you mark it with internal linkage. Internal linkage means that multiple identifiers refer to different things, even if they appear in different translation units (in C, a translation unit is a source file after it has been processed by the C preprocessor). To put in another way, an identifier with internal linkage can only be seen from the translation unit where it has been declared.

You should use it when you don't want entities in other translation units to know about a particular identifier in your source file. When you don't have many entities depending on your constant, you can change it as you like while minimizing the impact in your program.

Daniel Martín
  • 7,815
  • 1
  • 29
  • 34
  • I think your description of internal linkage is the opposite of what you meant to say. Identifiers with internal linkage will refer to *different* things in different translation units. – Chuck Apr 14 '14 at 21:51
1

For variables in the top-level scope, static means that the variable has internal linkage. So two files that declare a static variable with the same name will each have their own variable rather than conflicting.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • So if I had two classes with that top level variable without static, they'd conflict. How so? – Doug Smith Apr 15 '14 at 01:56
  • @DougSmith: Well, you'd have multiple definitions of the same global variable. C does not allow that, so you'll either get an error or undefined behavior. – Chuck Apr 15 '14 at 02:15
0

Objective-C is a superset of C, so static global variable means the same thing as in C. In C static makes a global variable visible only within the file it is declared in. You can read more about it here: What does “static” mean in a C program?

Community
  • 1
  • 1
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • @DougSmith: You don't import variables. `#import` is a compiler directive that copies the text from the named file into the place where the `#import` appeared. If you import a file that contains a static variable declaration, you'll just create another static variable declaration in place of the `#import`. – Chuck Apr 15 '14 at 02:24