As @Clashsoft says you can not initialize the class like this. You can do some thing simple like this for testing:
class Rational(n: Int, d: Int) {
def oneHalf: Int =
n * d
}
object MyProgram {
def main(args: Array[String]) {
val rational = new Rational(1, 2)
println(rational.oneHalf)
}
}
It is also possible to use App
trait (extends App
) then you do not need to have main method:
object MyProgram extends App {
val rational = new Rational(1, 2)
println(rational.oneHalf)
}
All depends on how you want to implement your solution at the end. Regarding different between main and App
trait please read more.
Thx to @tzachzohar for following addition:
In Scala, just like in Java, only a static main
method (with appropriate argument and return types) can serve as a program's entry point. For convenience, IntelliJ IDEA provides a Scala Worksheet as a way to easily test your code, but that's no magic either - it's a just nice wrapper - behind the scenes, a worksheet has its own main
method calling your code.