Yuriy Tymchuk wrote:
Can anybody tell me what is the difference between the class variable on the "instance sideâ definition and instance variable on the âclass sideâ definition?
Thanks in advance. Uko
instance-variable: A variable that holds the private state of an object. In Smalltalk these can only be accessed by instance-methods. class-variable: An attribute of a class that is shared by that class, its subclasses, and all instances of those classes. Can be accessed by both instance-methods and class-methods. class-instance-variable: An attribute of a class that is not shared outside that specific class. Only the definition is inherited by subclasses, not the value . Each subclass has its own private value. Accessible only by class-methods. You will understand it best by experimenting. Here is one I made for myself... ivCounter - instance variable counter CVCounter - class variable counter CIVCounter - class instance variable counter "----------" Object subclass: #TestCounter instanceVariableNames: 'ivCounter iName' classVariableNames: 'CVCounter' poolDictionaries: '' category: 'MyExamples' TestCounter class "accessed by clicking the <Class> button" instanceVariableNames: 'CIVCounter' TestCounter subclass: #TestSubCounter instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'MyExamples' "----------" TestCounter name: myInstanceName iName := myInstanceName. TestCounter >> next ivCounter ifNil: [ ivCounter := 0 ]. CVCounter ifNil: [ CVCounter := 0 ]. ivCounter := ivCounter + 1. CVCounter := CVCounter + 1. TestCounter >> printOn: aStream super printOn: aStream. aStream nextPutAll: ' ['. aStream print: iName. aStream nextPutAll: '] ( '. aStream nextPutAll: ' ivCounter='; print: ivCounter. aStream nextPutAll: ', CIVCounter=n/a'. aStream nextPutAll: ', CVCounter='; print: CVCounter. aStream nextPutAll: ' )'. "----------" TestCounter class >> next2 CIVCounter ifNil: [ CIVCounter := 0 ]. CVCounter ifNil: [ CVCounter := 0 ]. CIVCounter := CIVCounter + 1. CVCounter := CVCounter + 1. TestCounter class >> printOn: aStream super printOn: aStream. aStream nextPutAll: ' ( '. aStream nextPutAll: 'ivCounter=n/a'. aStream nextPutAll: ', CIVCounter='; print: CIVCounter. aStream nextPutAll: ', CVCounter='; print: CVCounter. aStream nextPutAll: ' )'. TestCounter class >> reset CVCounter := nil. CIVCounter := nil. "---------" Then from Workspace evaluate these... tc1 := TestCounter new. tc2 := TestCounter new. tsc1 := TestSubCounter new. tsc2 := TestSubCounter new. TestCounter reset. TestSubCounter reset. tc1 next. tc2 next. tc1 next. tc2 next. tsc1 next. tsc2 next. tsc1 next. tsc2 next. TestCounter next2. TestCounter next2. TestCounter next2. TestSubCounter next2. TestSubCounter next2. TestSubCounter next2. cheers -ben