views:

1007

answers:

1

I don't quite understand static variables when defined in the implementation of an interface. In methods I do understand how they differ from local variables, but not when defined directly in an implementation.

Look at these examples. What difference do these two make practically?

#include "MyClass.h"

@implementation MyClass
int myInt;
...
@end

And:

#include "MyClass.h"

@implementation MyClass
static int myInt;
...
@end

myInt is in both cases visible to all the methods, and if I interpreted a test I ran correctly, myInt will in both cases be the same variable for different instances of the class.

+4  A: 

The 'static' keyword in that context is the same as it would be in plain C: it limits the scope of myInt to the current file.

smorgan
Also, defining a non-local variable inside @implementation is no different from defining it outside.
sigjuice
OK cool. Thanks guys!
quano
But does this really make any difference for definitions in implementation files? They're not included anyways.
quano