Jack Jack - 7 months ago 55
Scala Question

Confused about Scala Singleton object

I'm a java programer and learning Scalanow. In Java a singleton object means that can only one of this type object exists in the whole JVM. But in Scala how could I create two singleton objects under same type, for example I could created two singleton objects in the same class type Seq:

val s1 = Seq("aa","bb")
val s2 = Seq("aa","bb","cc")


This is a bit different. Read documentation: http://docs.scala-lang.org/tutorials/tour/singleton-objects.html.

In your example you create a value of type Seq. It not a singleton! Here is example for singleton :

object Blah {
  def sum(l: List[Int]): Int = l.sum

This is a duplicate question.

Please read next thread: Singleton & Companion Objects