Static instance variables and class methods...
Static instance variables and class methods...
- Subject: Static instance variables and class methods...
- From: Jeff LaMarche <email@hidden>
- Date: Sat, 6 Oct 2001 22:29:00 -0700
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