Last updated at 12:03 pm UTC on 17 January 2006
A class method is a Method which defines operations which have access to the Class variables. They typically perform initialization and finalization functions for instances of the class. They are the primary way in which class variables are set, changed, or retrieved by other objects. They might also perform common operations associated with the class that do not need any instance to perform them such as utility functions involving conversion or constants.
is a message (new) to a Class (OrderedCollection) to create and return an instance of itself.
By default a class returns itself if no explicit return statement is provided by the class method body.
A utility function might be something like:
Integer primesUpTo: 30
#(2 3 5 7 11 13 17 19 23 29)
This could have been an Integer instance method
Shorter, but doesn't read quite as well.
And a method returning constants might be something like
Date nameOfDay: 6
Clearly an Integer instance method of "nameOfDay" could perform the same function:
but then you've placed date related functions in Integer which may be harder to locate than browsing the Date class.
Another example is:
Basically giving a convenient place to perform the function where one might logically expect to find it and keeps it close to the types of things it is used with (as opposed to the Global variable pool, for example).