Scala Tutorial - Scala Code Blocks








Method and variable definitions can be single lines as follows:

def meth() = "Hello World"

Methods and variables also can be defined in code blocks that are denoted by curly braces:{ }.

Code blocks may be nested.

The result of a code block is the last line evaluated in the code block as shown in the following example.

object Main {
    def meth1():String = {"hi"}
    def meth2():String = {
        val d = new java.util.Date()
        d.toString()
    }

  def main(args: Array[String]) {
    println(meth1 )
    println(meth2 )
  }
}

Variable definitions can be code blocks as well.

val x3:String= {
    val d = new java.util.Date()
    d.toString()
}