How to move main method to another class in Scala?

笑着哭i 提交于 2020-01-02 01:21:10

问题


IntelliJ IDEA 10.5 (probably this matters).

I am new to Scala, so I started in an akward way. I created one file with two classes -- empty MainApp and another class, HelloWorld with method main.

I compiled it and executed -- IntelliJ automatically detected HelloWorld as main class. It was OK.

Then, I moved main method to MainApp, and deleted (then empty) HelloWorld class. When I tried to run it, IntelliJ sticked to HelloWorld nevertheless. So I reconfigured project and selected MainApp as main class.

I tried to run it with such result:

MainApp main method should be static

I am completely puzzled. First of all, Scala does not have static methods. Second of all, why it does not compile now, when it compiled before (with HelloWorld class). I though that only requirement is having one main method.

Thank you in advance for your help.

Please note: I know I can start a new project from scratch to avoid the problem altogether, but I would like to learn something, i.e. get to know what is going on, and fixing this project.


回答1:


static methods in Java roughly correspond to singleton methods in Scala. You should have

object MainApp {
  def main(args : Array[String]) = ...
}

in your code, not class MainApp.



来源:https://stackoverflow.com/questions/7430235/how-to-move-main-method-to-another-class-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!