Friday, November 28, 2014
For programmers already familiar with C++ or Java the concept of object in Scala looks interesting. In Scala object is a keyword and implicitly creates a singleton which is guaranteed to be unique. There is a anonymous class created for every object. It is important to note that Scala doesn't have static methods or fields and object serves the same purpose.

A basic example of Scala object.

object MyObject {    
  def main (args : Array[String]) {    
    println("This is a Scala object")
  }
}

Scala object can have fields, methods and also extend super classes and implement interfaces.

object MyObject extends MySuperClass {
  // Fields
  var field1 = 10
  val field2 = 20
    
  def main (args : Array[String]) {      
    println("This is a Scala object")
    println(field2)
  }
}

Proceed to read other Scala Tutorials.

0 comments :

Post a Comment

Contact Form

Name

Email *

Message *

Back to Top