Main method not supported in scala companion object

I was working on some practice problems from the Ninety-Nine Scala Problems website when I stumbled across an interesting behavior.

I wrote a dummy class and a main method in its companion object.

package arithmetic {

    class S99Int(val start: Int) {
      import S99Int._

      def hello = println("hello")
    }

    object S99Int {

      def main(args: Array[String]) :Unit = {
          println("hello world")
      }

    }

}

To my surprise, I got a NoSuchMethodError: main from the compiler.

Turns out that this is not supported in Scala (as of now). Companion objects can’t have a main() method. There are some esoteric explanations available on the Scala mailing list for this behavior.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: