Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 14, 2010, 06:37 PM   #1
BadWolf13
macrumors 6502
 
Join Date: Dec 2009
What's with this "static" instance declaration?

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.

Code:
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?
BadWolf13 is offline   0 Reply With Quote
Old Oct 14, 2010, 06:40 PM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
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.

<edit>
That code, by itself, is broken though. It needs to be assigned non-nil *somewhere*. The usual pattern is:
Code:
static Foo *bar;
if (bar == nil) {
    bar = [[Foo alloc] init];
}
return bar;
</edit>
Catfish_Man is offline   0 Reply With Quote
Old Oct 14, 2010, 06:59 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
The posted code was a fragment. The actual code is not broken, AFAICT. It just inverts the usual idiom so there isn't a big block for the if-nil condition.

http://developer.apple.com/library/m...eListings.html
chown33 is offline   0 Reply With Quote
Old Oct 14, 2010, 07:42 PM   #4
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 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?
BadWolf13 is offline   0 Reply With Quote
Old Oct 14, 2010, 09:58 PM   #5
cubist
macrumors 68020
 
Join Date: Jul 2002
Location: Muncie, Indiana
Send a message via AIM to cubist
Actually, static on a global variable makes it private to that source module. Remember Cocoa is basically C, not C++.
cubist is offline   0 Reply With Quote
Old Oct 14, 2010, 10:20 PM   #6
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 2009
Quote:
Originally Posted by cubist View Post
Actually, static on a global variable makes it private to that source module. Remember Cocoa is basically C, not C++.
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.
BadWolf13 is offline   0 Reply With Quote
Old Oct 15, 2010, 07:12 AM   #7
knightlie
macrumors 6502a
 
Join Date: Feb 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.
knightlie is offline   0 Reply With Quote
Old Oct 15, 2010, 10:48 AM   #8
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by BadWolf13 View Post
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.
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Oct 15, 2010, 12:56 PM   #9
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 2009
Quote:
Originally Posted by Sydde View Post
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.
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?
BadWolf13 is offline   0 Reply With Quote
Old Oct 15, 2010, 01:43 PM   #10
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 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:

Code:
class TestClass {

public static String MyVar1 = "hello";
public string MyVar2 = "world";

public TestClass() {}

}
In this case, to get at MyVar1, you can just do:

System.out.println(TestClass.MyVar1);

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

System.out.println(TestClass.MyVar2);

You would get a null pointer.

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

TestClass tc = new TestClass();
System.out.println(tc.MyVar2);

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.
ulbador is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
2013 iMac static "clicking"? snapdragonx iMac 9 May 12, 2014 03:45 AM
Resolved: Static/Humming from internal PBG4 15" 1.67 gavinstubbs09 PowerPC Macs 3 Mar 7, 2014 08:48 PM
Instance variable declaration: best practices? MacMan988 iPhone/iPad Programming 1 Jan 26, 2014 07:21 AM
The app switcher "card" isn't just a static screen capture Gogurt48 iOS 7 6 Jul 4, 2013 12:19 AM
Accessing a static variable from an instance method nerak99 iPhone/iPad Programming 25 Sep 13, 2012 11:26 AM

Forum Jump

All times are GMT -5. The time now is 07:07 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC