Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move main method to another class in Scala?

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.

like image 444
greenoldman Avatar asked Sep 15 '11 11:09

greenoldman


1 Answers

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.

like image 83
jpalecek Avatar answered Nov 13 '22 07:11

jpalecek