[Newbies] class variable vs class instance variable uses

Miguel Enrique Cobá Martinez miguel.coba at gmail.com
Tue Aug 4 15:28:07 UTC 2009


What is the difference between a class variable like Current in Locale
class:

Object subclass: #Locale
	instanceVariableNames: 'id shortDate longDate time decimalSymbol
digitGrouping currencySymbol currencyNotation measurement
offsetLocalToUTC offsetVMToUTC dstActive'
	classVariableNames: 'Current CurrentPlatform KnownLocales
LanguageSymbols LocaleChangeListeners PlatformEncodings'
	poolDictionaries: ''
	category: 'System-Localization'

that is accessed as (in class side):

current
	"Current := nil"
	Current ifNil: [
		Current := self determineCurrentLocale.
		"Transcript show: 'Current locale: ' , Current localeID asString;
cr"].
	^Current

and current in SmalltalkImage class side:

SmalltalkImage class
	instanceVariableNames: 'current'

that is accessed as:
current
	"Note that this could be implemented differently to avoid the test"

	current isNil
		ifTrue: [current := self basicNew].
	^ current

I'm trying to create a singleton but I have some doubts in the correct
implementation.

I have also seen the explanation on 

http://coweb.cc.gatech.edu/cs2340/3872

also:

http://stackoverflow.com/questions/438729/smalltalk-singleton-pattern-how-do-i-initialize-the-instance-variables

that corresponds to the first type, using (I suppose) a class variable
and

http://wiki.squeak.org/squeak/939

that corresponds to the second type.

Are there guidelines or pros/cons about singletons on Smalltalk?

Thanks,
Miguel Cobá



More information about the Beginners mailing list