Hi Gary,

 

This is a good question.

 

The code is probably lazily instantiating the variable so that it can be used somewhere else.   If the class variable was private to the class and not intended to be used by another class and you were only planning on calling it from the class side then it kinda works.  If not; you plan to have an instance call the class variable, or another class needs access to this Class variable, then you need some sort of accessor.

 

Have a look at SmalltalkImage class variable StartUpList.  There is no public accessor for it.  There are values in it.  Can you find a way to access that list and see what is in it?

 

You can write code on the class side that references StartUpList (press the class variables button to see the methods that do that, or even highlight StartUpList in a class method of that object and select: Inspect.) but can you find another way?

 

In many cases you need to access the class variable to get information that is relevant to all the class instances, or to get a single class instance (MyObject class > myDefaultInstnace) from another class so class variable accessors are required.

 

Ron Teitelbaum

 

 

 

 

 

From: beginners-bounces@lists.squeakfoundation.org [mailto:beginners-bounces@lists.squeakfoundation.org] On Behalf Of Gary Dunn
Sent: Monday, February 28, 2011 3:07 PM
To: Squeak Beginners
Subject: [Newbies] Returned values

 

While studying DBus code I came across a method that appears to set a class variable and return the value. In DBusConnection>> sessionBus

  ^SessionBus := self getBus: 0 from: SessionBus

and SessionBus is a class variable. Why return the value? Why not have the sender examine the value of SessionBus? Is it only a matter of style or is this more efficient?

Gary Dunn
Open Slate Project
http://openslate.org