Sunday 24 December 2017

Introduction to Scala - VIII

We continue with more Scala on the topic of Singleton Objects in this post. For all the work in this post, we will use Read-Evaluate-Print-Loop Scala interpreter. Some familiarity with Java will be of great help in understanding Scala. In this post, we will look at the way  Singleton Objects are declared and used in programs.

A Singleton Object is just like a class but, as the name says there can only one object or instance. We begin by taking a simple example as shown below:

object FirstObject {
def print {
println("Hello World!")
}
}


The results are shown below:

scala> object FirstObject {
     | def print {
     | println("Hello World!")
     | }
     | }
defined object FirstObject


When we try to instantiate it, we get below error:

scala> val Object = new FirstObject
<console>:11: error: not found: type FirstObject
       val Object = new FirstObject


To run it, we run below command as we normally run a static method in Java:

scala> FirstObject.print
Hello World!


 We already had a look at Singleton objects in the first post.The program is reproduced below:

object foobar {
/*Objects are single instances of their own definitions.
  It contains a method called main, the entry point of the program */
  def main(args: Array[String]){
    println("Hello, World!")
    // prints Hello, World
  }
}


The above program can be rewritten as shown below:

object foobar extends App{
    println("Hello, World!")
  }


Saving above lines in a file called AppDemo.scala and running it gives same result as above program:

F:\scala\examples>scalac AppDemo.scala

F:\scala\examples>scala -classpath . foobar
Hello, World!


In Scala, there is no concept of static fields or static methods. Instead, we have an object that can be used to define a single instance of a class with the desired functionality.

object foobar extends App{
    println("Hello, World!")
  }

In the above program, the main method from App trait is inherited. Two points of caution: The main method cannot be overridden and the whole body of the program is the main method itself.


The next topic we will see is that of Companion Objects. As mentioned before, in Scala classes do not have static fields or static methods. The way to implement static fields or static methods is to define a Companion Class and a corresponding Companion Object having the same name. Companion Objects have access to even the private members of the associated Companion Class. A simple example is shown below where a Companion Class and a Companion Object are defined. The members of the Companion Object are like static members of the Companion Class.

class Companion
object Companion {
var x:Int = 1
def Print:String = { "Hello, World!" }
}


The results are shown below:

scala> class Companion
defined class Companion

scala> object Companion {
     | var x:Int = 1
     | def Print:String = { "Hello, World!" }
     | }
defined object Companion
warning: previously defined class Companion is not a companion to object Companion.
Companions must be defined together; you may wish to use :paste mode for this.


Let is try to run using :paste as shown below:

scala> :paste
// Entering paste mode (ctrl-D to finish)

class Companion
object Companion {
var x:Int = 1
def Print:String = { "Hello, World!" }
}

// Exiting paste mode, now interpreting.

defined class Companion
defined object Companion


Now, the members can be accessed as though they are static members of Companion as shown below:

scala> Companion.x
res0: Int = 1

scala> Companion.Print
res1: String = Hello, World!


This concludes the discussion on Companion Objects.