Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method being executed out of order in Scala

Tags:

scala

I don't know what am I missing , may be its something minor, but here is the problem

println() is being called out of order. i.e. Output that I get is

Line two.

Line one

Shouldn't it be reversed?

I do know that that "Line two" is out of main method, but what is the reason, I tried searching, but couldn't get to the bottom.

object oneToTen {
    def main(args: Array[String]): Unit = {
    println("Line one")
  }

  println("Line two.")
}
like image 622
finickyTunnel Avatar asked Dec 04 '25 17:12

finickyTunnel


1 Answers

In Scala, everything that is in the body and that is not a variable declaration is part of the constructor. As oneToTen is a scala object, an instance of it is created and then the main method is invoked.

like image 84
user3001 Avatar answered Dec 06 '25 09:12

user3001