Why does Scala start with an object main method instead of a static main class method?

In Java, a program is run from the main static method of a class. But in Scala it starts with the main method of the object.

Does anyone know the rationality of such a design?

Scala also provides a trait App

that can replace a method main

if the object extends that trait. Does anyone have any ideas as to which one is ( App

or main

) the preferred way?

Thank!

+3


source to share


4 answers


Using static is similar to using global functions. Scala is purely object oriented and has no statics. But actually the Scala compiler generates a static main for the JVM. For short training programs it is recommended to use the "App" function.



+1


source


Scala has singleton objects instead of java static members. Thus, the main method of an object is analogous to the static main method of java.



+7


source


Scala has no static methods. If Scala needs to run programs from a static main method, you can never run a Scala program because you cannot write a static main method.

+3


source


I'm always extending App

in my main class and don't know the reason why anyone would want to write an explicit main method - except perhaps for compatibility with some other code that was too fussy about such things. Whenever I see an explicit main method in Scala, I wonder if the code is very old (pre-existing App

) or if the author was new to Scala.

But this question is mostly related to why the Scala designers left out static methods. You can find this question on how static members are not object oriented .

+3


source







All Articles