Help me out with the static variable type


#1

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?

…Dale


#2

I think you are reading far beyond the meaning of the static variable declarations.

Static variables are not initialised over and over while the program in which they are defined is running. (Anything outside functions or methods are not executable.)

The static variables are initialised only once. The compiler generates code to make sure that this happens before a static variable is accessed.

...
// A global static variable, global to the module in which it appears;
// not visible from outside the module.
static int counter = 16;

void decrement ()
{
       counter -= 1;
       // starts out as 16 but gets decremented each time the function is called
       // 15, 14, 13, ...
}

void decrementHidden ()
{
       // A different static variable despite having the same name as the global one
       // Private to this function; not visible from outside
       static int counter = 32;

       counter -= 1;
       // starts out as 32  but gets decremented each time the function is called
       // 31, 30, 29, ...
}

(Yes you can declare (private) static variables inside functions or methods.)


#3

The line of code that creates the variable is only executed once. Technically, it’s not even executed at all, but conceptually, you can think of it that way.

Most variables have a lifetime - an isntance variable lasts as long as its object lives, a local variable lasts as long as the method it was declared in is running - static variables live throughout the life of the application. So, it starts as nil, when that method runs, the if statement succeeds and the variable is then set to point at an object. The next time the method runs, the if statement fails because the variable points at something.

You could move the variable declaration to outside of the method. You could potentially move it to a different file with a little extra syntactical care. However, declaring it “inside” the method means that you can only access it from that method, even though the memory for it is global.


#4

Thanks Joe:

It’s just this line that kept tripping me up:

Because it’s static, the entirety of that line is run only once. I’m gathering then, that the following would yield very different results:

static BNRItemStore *sharedStore; sharedStore = nil;

Setting sharedStore to nil doesn’t happen over and over in the first example because it is set to nil as the static variable is created. But am I correct that if I set it to nil on the very next line then it would be set to nil over and over and over again each and every time that method is called?

…Dale


#5

Just try it in Xcode and see what happens; you can not set off a nuclear reaction that will result in a meltdown.


#6

Yes, it would be set to nil each time you ran the method in this case.