In the method:
the first thing that was done was declare the static variable sharedStore to be nil as follows:
Below that the text says “a static variable is only declared once (when the application is loaded into memory)”.
Does this mean then, that without any object having called the sharedStore method in the BNRItemStore class, when the program is first executed, the application looks through every method of every object in search of static variables to declare them?
The reason I ask is because when I follow the logic of the sharedStore method, it seems to me that each time the method is called the sharedStore pointer variable will be set to nil, over and over again. If it is set to nil, then wouldn’t that result in sharedStore being instantiated over and over again because it’s always set to nil and the
would always return true? Or is the line:
simply ignored every time the sharedStore method is called because somehow, magically, it knows that that variable was already called on program load and therefore the line is just skipped.
The text says (a couple paragraphs above Figure 9.7) that
Yes, it will until it hits the line
which should set it back to nil.
Does Objective C work in this way:
- At launch every static variable everywhere in every object is declared; and
- Thereafter every line of code that declares a static variable is just ignored
If that is the case it seems very odd. Why would they declare static variables in-line. I should think it would make sense to declare them once in a header instead of putting the declaration within code that could be executed multiple times.
What am I missing?