Question about static methods and inheritance


I have a question concerning the code below:

open class A() {   class object {   fun foo() {            print("A")   }   } }

class B() : A() {
  class object {
  fun foo() {
  // compiles and prints “AB”
  // does not compile

fun main(args: Array<String>) {

Would be nice if in B’s class object would compile. But it doesn’t as in Java. This was always something that annoyed about Java. When I change the hierarchy I have to go into some refactoring work. Astonishingly, noby outside the Smalltalk world ever complained about this. Maybe people see this as natural, because it is the same way in C++. I can understand that “myClass allSubclassesDo: aBlock” as in Smalltalk doesn’t work in Java as not all existing subclasses may be loaded into the code space when iterating over all subclasses and calling static methods. But the other way round in the hierarchy is a different thing.

This is not too important, I know. But it would make things a little more intuitive. Another thing are class instance variables: for every subclass some class variable has a different contents as if every subclass had a new instance of that inherited class variable (hence the name). Also, nobody outside the Smalltalk world things this is a must have, I know. But would be nice … :wink:

Regards, Oliver

What is the advantage of "companion object" vs static keyword

the Java Puzzlers book shows how bad it is when static methods are inherited. It is a consious decision not to have it in the language.


This is the first time I hear about problems with static methods and inheritance. In almost 10 years of Smalltalk development I never ran into a situation that caused problems with this and I never heard of any colleague at work about problems with it.

Maybe there are pitfalls when both, static methods and static variables, are no true class methods and class variables as they aren’t in Java (unlike Smalltalk). Strange, but not an important issue.