Consider the following Objective-C code:
@interface ClassA : NSObject {
}
-(void) printVal;
@end
@implementation ClassA
-(void) printVal {
int val;
NSLog(@"%i", val);
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ClassA* cA = [[ClassA alloc] init];
[cA printVal];
[cA printVal];
[cA printVal];
[pool drain];
return 0;
}
Why is this output:
2012-11-29 22:12:06.586 TestOne[20266:903] 0
2012-11-29 22:12:06.587 TestOne[20266:903] 32767
2012-11-29 22:12:06.588 TestOne[20266:903] 32767
In other words, why is val
not reinitialized to 0 when it is redeclared, and why does it receive the value 32767
every subsequent time the method is called?