52

I know you can define them indirectly achieve something similar with companion objects but I am wondering why as a language design were statics dropped out of class definitions.

Kipton Barros
  • 21,002
  • 4
  • 67
  • 80
numan salati
  • 19,394
  • 9
  • 63
  • 66
  • 2
    My guess is that without `static`, the overall design of Scala simplifies. We already have classes, and objects which are instances of classes, so the idea of a singleton object (a class which is only instantiated once) is very natural and simple. By the way, at the JVM level, singleton objects really are single instances of a class. The methods on a singleton object don't compile to `static` methods. This can be useful for things like defining a singleton object that inherits from traits. – Kipton Barros Sep 04 '11 at 21:38
  • 1
    @kipton nice thinking, and pretty close to the mark. The original decision was to drop statics (as being not-OO, and with a couple of namespace concerns). Singleton objects were then added to the language as a better way to fill the role that statics performed in Java (and other C++ derivatives). – Kevin Wright Sep 04 '11 at 23:17
  • I edited the question to clarify that members of Scala's singleton objects are not really the same as Java's `static` members. – Kipton Barros Sep 05 '11 at 00:41
  • @kipton, actually if you look into the resulting bytecode you will see that object is compiled to a class with static methods. – numan salati Sep 05 '11 at 01:31
  • @numan, Object compiles to class with static members? From the REPL, I tried this: `object Foo { def x = 1 }` and then `:javap -v Foo`. I get `public int x(); ...` without the `static`. Compare to, say, `:javap -v java.lang.Integer`, where the static methods are apparent. Am I missing something? Scala 2.9.1. In any case, Scala's companion object has different semantics than Java's static members. – Kipton Barros Sep 05 '11 at 03:40
  • @Kipton, i just compiled your example with scalac (2.9). ran "javap -c Foo". I see "public static final int x();" If you run javap on the inner class "Foo$" i see "public int x();". It seems that Foo delegates to Foo$ where "x" is invoked virtually. Not sure why it needs to do that... – numan salati Sep 05 '11 at 04:27
  • 3
    @Numan - The so-named "static forwarders" are implemented specifically to support Java interop, those methods are not used internally by Scala. – Kevin Wright Sep 05 '11 at 05:47

5 Answers5

58

The O in OO stands for "Object", not class. Being object-oriented is all about the objects, or the instances (if you prefer)

Statics don't belong to an object, they can't be inherited, they don't take part in polymorphism. Simply put, statics aren't object-oriented.

Scala, on the other hand, is object oriented. Far more so than Java, which tried particularly hard to behave like C++, in order to attract developers from that language.

They are a hack, invented by C++, which was seeking to bridge the worlds of procedural and OO programming, and which needed to be backwardly compatible with C. It also admitted primitives for similar reasons.

Scala drops statics, and primitives, because they're a relic from a time when ex-procedural developers needed to be placated. These things have no place in any well-designed language that wishes to describe itself as object-oriented.


Concerning why it's important to by truly OO, I'm going to shamelessly copy and paste this snippet from Bill Venners on the mailing list:

The way I look at it, though, is that singleton objects allow you to do the static things where they are needed in a very concise way, but also benefit from inheritance when you need to. One example is it is easier to test the static parts of your program, because you can make traits that model those parts and use the traits everywhere. Then in the production program use a singleton object implementations of those traits, but in tests use mock instances.

Couldn't have put it better myself!

So if you want to create just one of something, then both statics and singletons can do the job. But if you want that one thing to inherit behaviour from somewhere, then statics won't help you.

In my experience, you tend to use that ability far more than you'd have originally thought, especially after you've used Scala for a while.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • Actually, statics in Java are inherited indeed. – Daniel C. Sobral Sep 04 '11 at 23:31
  • 6
    @Daniel - It's debatable, inheritance has a fairly specific meaning in OO. I'd describe statics as being pulled into scope, not inherited. The difference becomes apparent when discussing static fields as opposed to static methods. An inherited thing can also be truly overridden, instead of being merely shadowed. – Kevin Wright Sep 05 '11 at 00:06
  • 2
    static methods are inherited, they are just not dynamically dispatched. they are resolved at compile time. how does defining them in object in scala help you with inheritance or polymorphism - two main drawbacks you state with java's approach? – numan salati Sep 05 '11 at 01:11
  • 1
    @numan You can put them in a trait, which is similar to, and generates, a Java interface. That means you can code to the interface, provide the singleton as an instance implementing the interface, etc. – Daniel C. Sobral Sep 05 '11 at 02:05
  • @Daniel, if i want dynamic dispatch i can use an instance method. i cant think of an example where polymorphic static methods would make sense, so in my mind this is not really an advantage. – numan salati Sep 05 '11 at 05:19
  • @numan - You can't fault the lack of static methods, then state that you, yourself, would abandon them (exactly what singletons do) when presented with the issues they solve. That's either plain disingenuous, actively trolling, or both. – Kevin Wright Sep 05 '11 at 05:45
  • @kevin, you are missing my point by a mile. and asking question to learn is hardly trolling. socrates would be mad at you. – numan salati Sep 05 '11 at 05:50
  • @Numan - I'm fairly confident that Socrates wasn't familiar with the term, it's behaviour that only seems to occur when people are offered the anonymity of online interactions. – Kevin Wright Sep 05 '11 at 06:09
  • @Kevin, all right dude you win. but i do want you to take a look at this http://en.wikipedia.org/wiki/Socratic_method. good day mate! – numan salati Sep 05 '11 at 06:30
  • 1
    @KevinWright what a crazy rant. Being object-oriented is almost literally the *opposite* of the goal of Scala. Scala attempts to be functional, and being functional is somewhat at odds with being object-oriented. Scala allows a mixture of the two. Java, too, allows a mixture of new and old. – T.W.R. Cole Oct 14 '14 at 19:04
  • @T.W.R.Cole Agreed, this post needs some down-voting, the answer isn't awful, but misleading and inferior to the selected answer. The rant only makes it worse. – Anthony Sep 21 '17 at 13:16
17

I also posted this question on scala users google group and Bill Venners one of the authors of "Programming in scala" reply had some insights.

Take a look at this: https://groups.google.com/d/msg/scala-user/5jZZrJADbsc/6vZJgi42TIMJ and https://groups.google.com/d/msg/scala-user/5jZZrJADbsc/oTrLFtwGjpEJ

Here is an excerpt:

I think one goal was simply to be simpler, by having every value be an object, every operation a method call. Java's statics and primitives are special cases, which makes the language more "complicated" in some sense.

But another big one I think is to have something you can map Java's statics to in Scala (because Scala needed some construct that mapped to Java's statics for interop), but that benefits from OO inheritance/polymorphism. Singleton objects are real objects. They can extend a superclass or mix in traits and be passed around as such, yet they are also "static" in nature. That turns out to be very handy in practice.

Also take a look at this interview with Martin Odersky (scroll down to Object-oriented innovations in Scala section) http://www.artima.com/scalazine/articles/goals_of_scala.html

Here is an excerpt:

First, we wanted to be a pure object-oriented language, where every value is an object, every operation is a method call, and every variable is a member of some object. So we didn't want statics, but we needed something to replace them, so we created the construct of singleton objects. But even singleton objects are still global structures. So the challenge was to use them as little as possible, because when you have a global structure you can't change it anymore. You can't instantiate it. It's very hard to test. It's very hard to modify it in any way.

To Summarize:

From a functional programming perspective static members are generally considered bad (see this post by Gilad Bracha - the father of java generics. It mainly has to do with side effects because of global state). But scala had to find a way to be interoperable with Java (so it had to support statics) and to minimize (although not totally avoid) global states that is created because of statics, scala decided to isolate them into companion objects.

Companion objects also have the benefit of being extensible, ie. take advantage of inheritance and mixin composition (separate from emulating static functionality for interop).

numan salati
  • 19,394
  • 9
  • 63
  • 66
4

These are the things that pop into my head when I think about how statics could complicate things:

1) Inheritance as well as polymorphism would require special rules. Here is an example:

// This is Java
public class A {
  public static int f() {
    return 10;
  }
}

public class B extends A {
  public static int f() {
    return 5;
  }
}

public class Main {
  public static void main(String[] args) {
    A a = new A();
    System.out.println(a.f());
    B b = new B();
    System.out.println(b.f());
    A ba = new B();
    System.out.println(ba.f());
   }
}

If you are 100% sure about what gets printed out, good for you. The rest of us can safely rely on mighty tools like @Override annotation, which is of course optional and the friendly "The static method f() from the type A should be accessed in a static way" warning. This leads us to

2) The "static way" of accessing stuff is a further special rule, which complicates things.

3) Static members cannot be abstract. I guess you can't have everything, right?

And again, these are just things which came to my mind after I gave the matter some thought for a couple of minutes. I bet there are a bunch of other reasons, why statics just don't fit into the OO paradigm.

agilesteel
  • 16,775
  • 6
  • 44
  • 55
2

Object oriented programming is all about objects and its states(Not touching state full and stateless objects in Java). I’m trying to stress “Static does not belong to objects”. Static fields cannot be used to represent a state of an object so it’s rational to pull off from objects.

Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47
2

It's true, static member don't exists, BUT, it's possible to associate a singleton object to each class:

class MyClass {
}

object MyClass {
}

to obtain similar results

Simone
  • 2,261
  • 2
  • 19
  • 27