They‘re very different! The one in @implementation
is a global variable not unique to each instance. Imagine there were accessors for both variables, written in the obvious way. Then the difference in behavior is shown here:
Someclass* firstObject = [[Someclass alloc] init]; Someclass* secondObject = [[Someclass alloc] init]; //forExample is an instance variable, and is unique to each instance. [firstObject setForExample:@"One"]; [secondObject setForExample:@"Two"]; NSLog(@"%@",[firstObject forExample]); //Result: "One" NSLog(@"%@",[secondObject forExample]); //Result: "Two" //anotherExample is a global variable, and is NOT unique to each instance. [firstObject setAnotherExample:@"One"]; [secondObject setAnotherExample:@"Two"]; NSLog(@"%@",[firstObject anotherExample]); //Result: "Two" (!) NSLog(@"%@",[secondObject anotherExample]); //Result: "Two" //Both instances return "Two" because there is only ONE variable this time. //When secondObject set it, it replaced the value that firstObject set.
If you are looking for this sort of behavior, you might be better off using a class variable, like this:
static NSString* yetAnotherExample = nil;
Then you can use class methods to interact with the variable, and it‘s clearly class-specific (as opposed to instance-specific or global).
instance-specific variable declared at @interface
global variable variable declared at @implementation
class variable variable declared using static
static variables in Objective-C
In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program. This is in contrast to automatic variables, whose lifetime exists during a single function call; and dynamically-allocated variables like objects, which can be released from memory when no longer used. More simply put, a static variable‘s value is maintained throughout all function/method calls. When declared outside of a function, a static variable is visible to everything within the file in which it is declared; when declared inside a function or method, it is visible only within that function or method, but the value is retained between calls.
Say you have this:
int f(void)
{
int i = 5;
i += 10;
return i;
}
Every call to f()
will return the value 15
.
Now say you have this:
int g(void)
{
static int i = 5;
i += 10;
return i;
}
The first time g()
is called, the value 15
will be returned. The second time, 25
will be returned, as i
maintained its value of 15
and then incremented itself by 10
. The third call, 35
will be returned. And so on.
In the context of Objective-C classes, static variables are often used to mimic class variables, as Objective-C does not have class variables (other languages, such as Java, do). For instance, say you want to lazily initialize an object, and only return that object. You might see this:
static MyObject *obj = nil;
@implementation MyObject
+ (id)sharedObject
{
if (obj == nil) obj = [[MyObject alloc] init];
return obj;
}
@end
obj
will be initialized the first time classObject
is called; subsequent invocations of classObject
will return the same object. You could check this by logging the address of the object:
NSLog(@"obj is at %p", [MyObject sharedObject]);
NSLog(@"obj is at %p", [MyObject sharedObject]); // Will print the same address both times
Furthermore, obj
will be visible to all methods in MyObject
.
This technique is used to implemented singleton classes in Objective-C as well.