[Newbies] Howto initialize class variables

Bert Freudenberg bert at freudenbergs.de
Sat Mar 20 09:10:10 UTC 2010

On 20.03.2010, at 09:46, Alex Schenkman wrote:
> Hi List!
> I'm playing with class variables. How should I initialize them?
> I've tried with the class method initialize but it does not do the job.
> Pirulo class>>initialize
>     super initialize.
>     myClassVar := Dictionary new at: 'one' put: 1.

* never call "super initialize" on the class side
* class variables should be capitalized
* class initializers need to be executed manually
* they are only executed automatically when loading the class (e.g. file-in or via Monticello)

So this would be right:

Pirulo class>>initialize
    "self initialize"
    MyClassVar := Dictionary new at: 'one' put: 1.

The "self initialize" comment is conventionally put there so you can easily double-click after the opening quotes to select the whole expression, and do-it. You need to do it any time you change the initialize method. This works because when evaluating code in a Browser, "self" refers to the currently selected class.

- Bert -

More information about the Beginners mailing list