15

Possible Duplicate:
Where do I have to declare static variables?

I've seen code like

@implementation ClassA

static NSString *str = nil;

.....

@end

as well as

static NSString *str = nil;

@implementation ClassA

.....

@end

What's the difference if a static var is declared inside the @implmentation context vs outside

Community
  • 1
  • 1
blueether
  • 3,666
  • 4
  • 26
  • 32

1 Answers1

16

There is no difference between

@implementation ClassA

static NSString *str = nil;

.....

@end

and

static NSString *str = nil;

@implementation ClassA

.....

@end

They work the same way ...

Static variables help give the class object more functionality than just that of a "factory" producing instances; it can approach being a complete and versatile object in its own right. A class object can be used to coordinate the instances it creates, dispense instances from lists of objects already created, or manage other processes essential to the application. In the case when you need only one object of a particular class, you can put all the object's state into static variables and use only class methods. This saves the step of allocating and initializing an instance.

zachzurn
  • 2,161
  • 14
  • 26
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147