Category:Smalltalk: Difference between revisions

Line 352:
====Globals / Namespaces====
By convention, global and namespace variables are to be used for classes only. As in every other language, it is considered extremely bad style to use globals for anything else.
They are created by telling the namespace to add a binding, as in <lang smalltalk>Smalltalk at:#Foo put:<something></lang>. The binding's value can be retrieved by asking the namespace: <lang smalltalk>x := Smalltalk at:#Foo</lang> or simply by referring to the binding by name: <lang smalltalk>x := Foo</lang>.
The binding's value can be retrieved by asking the namespace: <lang smalltalk>x := Smalltalk at:#Foo</lang> or simply by referring to the binding by name: <lang smalltalk>x := Foo</lang>
As seen above, the global named <tt>Smalltalk</tt> refers to the Smalltalk namespace, which is globally visible (technically, there is a binding to itself).
 
====Class Variables (Statics)====
These are visible inside a class and shared with all of its subclasses. They have the lifetime of the defining class. The binding is shared with all subclasses. Typically, these are used for constants. Class variables are defined in the class definition message when the class is instantiated or redefined by setters to the class.
Anonymous user