3

I did some research about static variables in objective-c and I found people declare static variables in different places which made me really confused.

  1. For the following code, can I put static NSUInteger counter;outside of the implementation? (Right after my #import "xxx.h")

  2. Can I put static NSUInteger counter;inside of +initialize class method?

  3. Can I put static NSUInteger counter; into a instance method?

  4. Most importantly, what are the differences and how to choose where to declare them?

Thanks!

@implementation MyClass

static NSUInteger counter;

+(void)initialize {
    if (self == [MyClass class]) {
        counter = 0;
    }
}

@end
2
  • I always go with below the import statements Commented Apr 3, 2014 at 23:06
  • 1
    There's no need to explicitly initialize a static variable to zero. Unless explicitly initialized to some other value, all static variables are automatically initialized to zero. Also, even if you did want to make the initialization explicit, there's usually no need to use +initialize method. Just use an initializer expression in the definition, like status NSUInteger counter = 1; or whatever. The only reason you might need to use a dynamic technique like +initialize is if you want to initialize to a value that's not a compile-time constant. Commented Apr 4, 2014 at 0:08

3 Answers 3

10

There are really only two versions out of the ones you provided. There is no difference between declaring them inside or outside the implementation block because static variables are not associated with the class but the file itself. There is also no difference between declaring them in a class method or instance method for the same reason.

The only difference between declaring them inside a method or not, is that if it is declared inside a method, it is only ever accessible from within that same method.

It is always best to declare a variable inside the most specific scope that you can so there is no risk of using it somewhere unintended causing bugs. If you only need to have access to the static variable inside a method, declare it there. Otherwise declare it anywhere else in the file (above where you need it). Where exactly you put it is purely a style thing at that point. I personally prefer it be after the imports but before the @implementation (and class extension if I add one).

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer! You mean "not associated with the class but the file itself" or "not associated with the instances but the class itself"?
He means "not associated with the class but the file itself."
6

The static attribute on a variable controls the lifetime of the variable and visibility of its name.

A static variable has the same lifetime as a global variable - the entire execution time of the application.

The visibility of a static variable depends on the location of its declaration:

  • If the declaration is within a file but outside of any function or method declaration then its name is visible throughout that single file.
  • If the declaration is within any block then the variables name is visible from its point of declaration throughout that block, including any enclosed blocks unless hidden by a declaration within such enclosed blocks. That is if the declaration occurs directly within the body of a method/function it is visible only within that method/function; if it occurs within the block of, say, an if statement then it is not visible outside of that block.

Comments

1

You can put it in all of those places. If the variable is in a method/function, then it will keep its value between calls. If it's in the top of a file, that means that it can only be accessed by code in that file.

A global variable that is accessed by multiple functions/methods in one file (but never in another file) is often declared as static to prevent conflicts with other variables with the same name in other files. If a variable needs to be accessed by a single method, and needs to keep it's value between calls, you can make the variable static and declare it inside the method to make your code more compact.

4 Comments

No matter what a static variable keeps its value between calls. Also, no matter what it can only be accessed by code in the file. Declaring it in a method just makes it even more restricted to just that method.
I was trying to explain the difference between static and normal variables.
How does it "prevent conflicts with other variables with the same name in other files"? Can you explain?
If there are two global variables with the same name, even in two completely unrelated files, you will get a linker error, but if one or both of the variables is static, then they can coexist without problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.