What's with this "static" instance declaration?

Discussion in 'Mac Programming' started by BadWolf13, Oct 14, 2010.

  1. macrumors 6502

    Dec 17, 2009
    Ok, working though the Core Data Utility Tutorial, and there's a bit of code here that works, but I don't understand it, so I was hoping someone could explain it to me.

    static NSManagedObjectModel *mom = nil;
    if (mom != nil) {
    	return mom;
    What I see is that it's creating an instance of NSManagedObjectModel and assigning it a nil value. Then it's asking if that instance is not equal to nil. How can it ever NOT be nil, if the line directly before the if statement assigns it a nil value? This is what I don't understand. Anyone got any clue?
  2. macrumors 68030


    Sep 13, 2001
    Portland, OR
    static is what's called a "storage class". It effectively makes that a global variable; initialized to 0 at program startup time, and only one copy ever instead of a new one each time the function is called.

    global variables are static by default.

    That code, by itself, is broken though. It needs to be assigned non-nil *somewhere*. The usual pattern is:
    static Foo *bar;
    if (bar == nil) {
        bar = [[Foo alloc] init];
    return bar;
  3. macrumors 604

    Aug 9, 2009
  4. thread starter macrumors 6502

    Dec 17, 2009
    So the use of the "static" at the beginning of the declaration basically causes the program to ignore the declaration and value assignment if the instance has already been declared?
  5. macrumors 68020

    Jul 4, 2002
    Muncie, Indiana
    Actually, static on a global variable makes it private to that source module. Remember Cocoa is basically C, not C++.
  6. thread starter macrumors 6502

    Dec 17, 2009
    Perhaps I should have specified. This code snippet is actually within a method, so if the "static" wasn't there, it would not have been a global.
  7. macrumors 6502a

    Feb 18, 2008
    The pattern behind this code is that the managedObjectModel() function is called whenever your program needs a managed object model, but the function will only create mom if it doesn't already exist - it's a Singleton pattern. If the static variable mom were set to nil then the next time managedObjectModel() was called it would recreate mom.
  8. macrumors 68000


    Aug 17, 2009
    Correct. Local variables live on the call stack, so they are transient. Static and global variables live in a reserved space at the bottom of the heap, just above constants. Declarations made within a routine or method are variables that cannot be seen outside that routine or method.

    If "static" were not there, mom would be a local variable, living on the stack. The "static" part tells the compiler to reserve heap space for mom and initialize it to nil with code placed near where the program starts, not when the routine is called.
  9. thread starter macrumors 6502

    Dec 17, 2009
    Wait, I may just be more confused, or I may just get it now. The "static" keyword there causes the compiler to move that line to the beginning of the program instead of at the beginning of the method?
  10. macrumors 68000


    Feb 11, 2010
    Although static variable are sort of global, it basically just means a variable or a method you can call without instantiating the class. Where I have used it the most is in Java, but it's going to be pretty similar in most languages. Take this class:

    class TestClass {
    public static String MyVar1 = "hello";
    public string MyVar2 = "world";
    public TestClass() {}
    In this case, to get at MyVar1, you can just do:


    Which would print out "hello". If you tried to do:


    You would get a null pointer.

    To get at MyVar2, you would actually be required to instantiate the class.

    TestClass tc = new TestClass();

    Which prints "world".

    Of course, your mileage may vary depending on the language, but as I said, it's going to be largely the same in most high level languages. As someone above mentioned, they can be huge memory hogs, since all static variables and methods for classes have to be allocated in memory when the program begins, not on demand.

Share This Page