Hi list,
can anyone please explain the difference between class variables at the instance side in the class browser and instance variables at the class side (thus of the metaclass) to me? And which of them is the preferred one? Regards, Martin |
Class variables are accessible within the instance and class sides.
class instance variables are accessible only within the class instance side. I essentially use class instance variables to keep track of created instances (e.g., singleton pattern). Cheers, Alexandre On 14 Jul 2008, at 15:27, Martin Beck wrote: > Hi list, > > can anyone please explain the difference between class variables at > the > instance side in the class browser and instance variables at the class > side (thus of the metaclass) to me? And which of them is the > preferred one? > > Regards, > Martin > -- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;. |
In reply to this post by Martin Beck-3
Martin Beck wrote:
> Hi list, > > can anyone please explain the difference between class variables at the > instance side in the class browser and instance variables at the class > side (thus of the metaclass) to me? And which of them is the preferred one? > > Regards, > Martin > > it makes a difference if you consider subclasses: a class variable is there only once, like a global variable, and accessable to the class and all its subclasses. a class instance variable is there for the class and each of its subclasses, and can have a different value for each subclass. i hope that makes sense cheers, wolfgang |
In reply to this post by Bergel, Alexandre
>>>>> "Alexandre" == Bergel, Alexandre <[hidden email]> writes:
Alexandre> Class variables are accessible within the instance and class sides. Alexandre> class instance variables are accessible only within the class Alexandre> instance side. Continuing on that, suppose you have Parent and Child classes, with the Child class being a subclass of Parent. If you define 'Home' as a class variable of Parent, you can use Home in both Parent and Child class *and* instance methods, and it'll refer to the single object defined in Parent class. (Class variables are always capitalized.) If you define 'office' as a class-instance variable of the Parent class, you can use it in Parent class methods only, not Parent instance methods. And, the Child class methods can also "use it", but they're referring to their own independent class-instance variable unique to the Child class. In other words, a class-instance variable is to a class, what an instance variable is to an instance: unique to the object. A class variable, on the other hand, is a bit like a global, but visible only to a class and its subclasses in all class and instance methods. As with any broad-scoped variable, use class variables only when needed, since it makes subclassing trickier and scope pollution and "action at a distance" more likely. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[hidden email]> <URL:http://www.stonehenge.com/merlyn/> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion |
In reply to this post by Martin Beck-3
On Monday 14 Jul 2008 6:57:10 pm Martin Beck wrote:
> Hi list, > > can anyone please explain the difference between class variables at the > instance side in the class browser and instance variables at the class > side (thus of the metaclass) to me? And which of them is the preferred one? Read 'class variables' as 'shared variables'. On any given side, classVariableNames: defines variables that are shared across all instances, while instanceVariableNames: defines variables that are unique to each instance. Both these sets of variables are accessible only to the methods defined on that side and its sub-classes. Classes are instances of a Metaclass but they are special in that there will be only one instance of a given class (stored in a global variable whose name is same as the class name), so there is no need for a separate classVariableNames: on the class side. Metaclass allInstances select: [ :c | c instVarNames size > 0 ] They do come in handy to track global state. For instance, SmalltalkImage class uses 'current' to track its singleton instance. ProjectLoading class uses it to track current world being loaded. Beeper uses one for the default sound to be played for system beep. Classes are global variables and their instance variables are also global. Unintentional modifications can result in weird side-effects (see comment in Player class>>scripts). So they are sparingly used. HTH, Subbu |
Free forum by Nabble | Edit this page |