C
Chris Uppal
isamura said:Perhaps I should drop the notion that static methods are analogous to
class methods. And that class methods don't exist in Java. Do correct me
if I am mistaken.
Well, I suggest a bit of both.
One the one hand, Java's classes are nothing like Smalltalk classes. In Java a
class is not an object, it is only a language construct. So many of the things
that you would naturally use a class object for in Smalltalk just don't make
sense in Java. E.g. you can't use a class as a factory object -- if you
need/want to use that pattern then you have to code up all the necessary stuff
yourself. As a result, all the "static" stuff has a semantics that will seem
unfamiliar to a Smalltalker -- "static" is not equivalent to Smalltalk's "class
side".
On the other hand, within fairly wide limits, Java's static members are used in
just the same way as Smalltalk's class-side method and classvars / class
instvars. Most, probably all, of the reasons for putting code on the class
side in Java would be equally valid in Smalltalk (the reverse is not always
true, since Java classes are not objects). So, more often than not, the
thinking is exactly the same. In fact I like to "pretend" that Java's classes
/are/ objects (distinct from their instances) when I'm deciding where to put
code -- decide which "object" is responsible for <whatever> and put the code on
the appropriate side of the fence.
Put it like this: it's perfectly reasonable to say that "static methods are
analogous to class methods", /provided/ that you hear in mind that the analogy
is rather loose and incomplete.
-- chris