36
votes

In Programming in Scala: A Comprehensive Step-by-Step Guide, the author said:

One way in which Scala is more object-oriented than Java is that classes in Scala cannot have static members. Instead, Scala has singleton objects.

Why is a singleton object more object-oriented? What's the good of not using static members, but singleton objects?

7
"orientated" is valid: worldwidewords.org/qa/qa-ori1.htm Though I must agree with you that I also prefer "oriented"Kevin Wright
Sorry about that confused you. Orientated is a British word for oriented.Sawyer

7 Answers

59
votes

Trying for the "big picture"; most of this has been covered in other answers, but there doesn't seem to be a single comprehensive reply that puts it all together and joins the dots. So here goes...

Static methods on a class are not methods on an object, this means that:

  1. Static members can't be inherited from a parent class/trait
  2. Static members can't be used to implement an interface
  3. The static members of a class can't be passed as an argument to some function

    (and because of the above points...)

  4. Static members can't be overridden
  5. Static members can't be polymorphic

The whole point of objects is that they can inherit from parent objects, implement interfaces, and be passed as arguments - static members have none of these properties, so they aren't truly object-oriented, they're little more than a namespace.

Singleton objects, on the other hand, are fully-fledged members of the object community.


Another very useful property of singletons is that they can easily be changed at some later point in time to not be singletons, this is a particularly painful refactoring if you start from static methods.

Imagine you designed a program for printing addresses and represented interactions with the printer via static methods on some class, then later you want to be able to add a second printer and allow the user to chose which one they'll use... It wouldn't be a fun experience!

12
votes

Singleton objects behave like classes in that they can extend/implement other types.

Can't do that in Java with just static classes -- it's pretty sugar over the Java singleton pattern with a getInstance that allows (at least) nicer namespaces/stable identifiers and hides the distinction.

11
votes

Hint: it's called object-oriented programming.

Seriously.

Maybe I am missing something fundamentally important, but I don't see what the fuss is all about: objects are more object-oriented than non-objects because they are objects. Does that really need an explanation?

Note: Although it sure sounds that way, I am really not trying to sound smug here. I have looked at all the other answers and I found them terribly confusing. To me, it's kind of obvious that objects and methods are more object-oriented than namespaces and procedures (which is what static "methods" really are) by the very definition of "object-oriented".

An alternative to having singleton objects would be to make classes themselves objects, as e.g. Ruby, Python, Smalltalk, Newspeak do.

2
votes

For static members, there is no object. The class really just is a namespace.

In a singleton, there is always at least one object.

In all honesty, it's splitting hairs.

2
votes

It's more object oriented in the sense that given a Scala class, every method call is a method call on that object. In Java, the static methods don't interact with the object state.

In fact, given an object a of a class A with the static method m(), it's considered bad practice to call a.m(). Instead it's recommended to call A.m() (I believe Eclipse will give you a warning). Java static methods can't be overridden, they can just be hidden by another method:

class A {
    public static void m() {
        System.out.println("m from A");
    }
}
public class B extends A {
    public static void m() {
        System.out.println("m from B");
    }
    public static void main(String[] args) {
        A a = new B();
        a.m();
    }
}

What will a.m() print?

In Scala, you would stick the static methods in companion objects A and B and the intent would be clearer as you would refer explicitly to the companion A or B.

Adding the same example in Scala:

class A
object A { 
  def m() = println("m from A") 
}
class B extends A
object B { 
  def m() = println("m from B")
  def main(args: Array[String]) {
    val a = new B
    A.m() // cannot call a.m()
  }
}
2
votes

There is some difference that may be important in some scenarios. In Java you can't override static method so if you had class with static methods you would not be able to customize and override part of its behavior. If you used singleton object, you could just plug singleton created from subclass.

-7
votes

It's a marketing thing, really. Consider two examples:

class foo
   static const int bar = 42;
end class

class superfoo
    Integer bar = ConstInteger.new(42);
end class

Now, what are the observable differences here?

  • in a well-behaved language, the additional storage created is the same.
  • Foo.bar and Superfoo.bar have exactly the same signatures, access, and so on.
  • Superfoo.bar may be allocated differently but that's an implementation detail

It reminds me of the religious wars 20 years ago over whether C++ or Java were "really" Object Oriented, since after all both exposed primitive types that aren't "really" objects -- so, for example you can't inherit from int but can from Integer.