Thursday, July 10, 2014

How to Write public static void main in Scala

psvm or public static void main(String agrs[]) is the starting method for any Java new bee.  This method is static as the signature suggests  , hence it can be invoked from the command line.  So if you are starting Scala you may be thinking about the method which is equivalent to this method.

Here is that signature -

def main (args : Arrays[String]) : Unit = {
  //method body
}

Here is an example in a class.

object HelloWorld {
   def main (args : Arrays[String]) : Unit = {
      println("Hello World")
   }
}

As may notice we started keyword object.  This is how you created Singleton object in Scala.  Isn't it simple?  The main method can now be easily mapped to public static void main.  Unit is a datatype which is equivalent to void in java.  It basically means this method does not have return value.

No comments:

Post a Comment