0 votes
in Scala Constructs by

What is “Application” in Scala or What is Scala Application? What is “App” in Scala? What is the use of Scala’s App?

1 Answer

0 votes
by

Scala Application: In Scala, App is a trait defined in scala package like “scala.App”. It defines main method. If an Object or a Class extends this trait, then they will become as Scala Executable programs automatically because they will inherit main method from Application. The main advantage of using App is that we don’t need to write main method. The main drawback of using App is that we should use same name “args” to refer command line argument because scala.App’s main() method uses this name. Example:- Without Scala App:

object MyApp {

    def main( args: Array[String]){

        println("Hello World!")

    }

}

With Scala App:

object MyApp extends App{

   println("Hello World!")

}

If we observe above two examples, in second example we have not defined main method because we have inherited from Scala App(Application). Before Scala 2.9, we have scala.Application trait. But it is deprecated by scala.App since Scala 2.9 version.

Related questions

0 votes
asked Sep 10, 2022 in Scala Constructs by Robin
0 votes
asked Sep 12, 2022 in Scala Constructs by Robin
...