Java – why do static members make the language not object-oriented?
I'm learning Scala. I came across this statement in odersky's programming Scala Second Edition:
I don't have enough experience with Java or Scala to understand this comparison Why do static members make languages less than OO?
Solution
Odersky's statement is valid and important, but some people don't understand what he means
Let's say that in Java, you have a class foo of method f:
class Foo { int f() { /* does something great */ } }
You can write a method that accepts Foo and calls f on it:
void g(Foo foo) { foo.f(); }
Perhaps a class subfoo extends foo; G also applies There can be a whole set of classes that share the fact that they can be used with G through inheritance or interface correlation
Now let's make the F method static:
class Foo { static int f() { /* does something great */ } }
We can use this new Foo and G, maybe so?
g(Foo); // No,this is nonsense.
damn. OK, let's change the signature of G so that we can pass foo to it and let it call F
Gee - we can't We cannot pass a reference to foo because foo is not an instance of a class Some people commented here are confused by the fact that the class object corresponding to foo, but as Sotirios tried to explain, the class object has no f method, and foo is not an instance of the class Foo is not an example of anything; It is not an object at all Foo's class object is an instance of class, which contains information about foo (think of it as Foo's internal Wikipedia page) and is completely irrelevant to the discussion Tiger's Wikipedia page is not a tiger
In Java, primitives like "3" and "X" are not objects They are objects in scala To improve performance, the program will use JVM primitives of 3 and 'x' as much as possible during execution, but at the level you write, they are actually objects In fact, they are not objects in Java and can have quite unfortunate consequences for anyone trying to write code that handles all data types - you must have special logic and other methods to override primitives If you've ever seen or written such code, you know it's terrible Odersky's statement is not "purism"; Far away
In Scala, no runtime data is not an object, and nothing can call methods that are not objects In Java, none of these statements are true; Java is a partially object - oriented language In Java, there are things that are not objects, and there are methods that are not on objects
Newcomers to scala often think that object foo is some strange alternative to Java static, but this is something you need to pass quickly On the contrary, the static method of Java is regarded as non OO wart, and the object foo {...} of scala is regarded as the following:
class SomeHiddenClass { ... } val Foo = new SomeHiddenClass // the only instance of it
Here foo is a value, not a type. It is indeed an object It can be passed to a method It can extend other classes For example:
abstract class AbFoo { def f:Int } object Foo extends AbFoo { def f = 2 }
Now, finally, you can say
g(Foo)
Indeed, the "companion object" of a class is a good place to place non instance methods and data for a class However, the companion object is an object, so the usual rules and functions apply
In fact, in Java, you place these methods on non objects – limiting how they are used – is a responsibility, not a function Of course not oo