Re: Static instance variables and class methods...
Re: Static instance variables and class methods...
- Subject: Re: Static instance variables and class methods...
- From: "John C. Randolph" <email@hidden>
- Date: Sat, 6 Oct 2001 22:46:01 -0700
On Saturday, October 6, 2001, at 10:29 PM, Jeff LaMarche wrote:
Okay, this may be a newbie question, but I'm trying to wrap my head
around DO and I'm a little confused about something:
When are static instance variables created? Can they be accessed in a
class method before the first instance of the class has been created?
What about after?
I was looking at Quinn "The Eskimo!"'s TrivialThreads and SimpleThreads
sample code from way back around Rhapsody DR2 (why hasn't Apple
released any new or updated NSThread or DO code for Cocoa??). The
examples use a class method to actually do the thread's task. The class
method creates and uses an instance of the class. A static instance
variable is used to keep track of the number of threads that have been
spawned.
What are the nuances of this? I know you're generally not supposed to
reference instance variables in a class method because a class method,
by definition, is not associated with an instance. Static variables,
however, are shared between all objects and it seems like the might be
available, at least under some circumstances, to a class method.
I'm guessing that it's safe to reference a static instance variable
from a class method if you have some way of being sure that at least
one instance has been created. Anyone care to either confirm this, or
verbally slap me around and let me know the way it really is?
As always, thanks...
Jeff,
I'm not sure what you mean by a static instance variable. An instance
variable is any variable declared in the structure following the
@interface declaration of a class, and "static" is not a legal storage
qualifier in that context.
For example, this is illegal:
@interface EightBitLUT : NSObject
{
static unsigned char bytes[255];
}
...
If you're referring to static variables in .m files, they behave just
like any other static variables in C, and they're *not* ivars.
-jcr
"I fear all we have done is to awaken a sleeping giant and fill him with
a terrible resolve." -Admiral Isoroku Yamamoto, Dec 7, 1941.