Scala Object

Scala Object

First thing first – Don’t get confused with the instance of an class.

When we create an instance of an class, we generally refer that as an object of class in other programming languages.

class Person
val person_instance = new Person
        

But here in Scala object is totally different concept. Let’s see some different aspect of this Scala object.

Class Level Functionality

One of the very fundamental aspect of OOP based languages. i.e. Functionality that does not depends on instance of class. See below example of Java.

class Person {

    public static final int n_eyes = 2;
}

class JavaClass {

    public static void main(String[] args) {
        System.out.println(Person.n_eyes);
    }
}        

In above code, we have easily accessed the n_eyes. As Java class gives you that functionality out of the box using static.

In Scala we have to create an instance in order to use class methods/members. As it doesn’t give you that class level functionality by default. i.e. Scala doesn’t have static values/method. Scala has something else, here comes the Object.

object Person {

// class level functionality
val n_eyes: Int = 2

}

class Person {

// instance level functionality

}        

Above example shows how we can achieve class level functionality in Scala. Now we can directly access Person.n_eyes wherever we import this class.

This is basically a companion object in Scala. An object that’s declared in the same file as a class, and has the same name as the class. A companion object and its class can access each other’s private members (fields and methods).

Singleton Object

An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val. Also we cannot instantiate an instance of Object class using new keyword.

Objects in Scala does not receive any parameters. But we can define members/methods inside. Like we do for classes.

object Person {
val n_eyes: Int = 2
}

val person1 =  Person
val person2 =  Person
println(person1==person2) // true        

Above equality statement shows that both are same instance only i.e only instance.

 Scala Application - Using Singleton Object

There are two ways to create an runnable application in Scala.

  • Define an Scala object with a main method defined in it.
  • Define an Scala object that extends the App trait.

Here if you see in below example, I have created a simple Scala Object which is runnable.

package com

object InvokeCode {

  def main(args : Array[String]): Unit ={
  println("Hello World !!")

  }
}        

Using App trait. In App trait, main method is already defined. Hence extending it makes below code runnable.

package com

object InvokeCodeOther extends App {

    println("Hello World !!")

}        

For more details please see official Scala website.

Happy Learning! Your feedback would be appreciated!

References

  • Daniel Ciocîrlan https://rockthejvm.com/ Scala 2 Course
  • https://docs.scala-lang.org/tour/singleton-objects.html

To view or add a comment, sign in

More articles by Shobhit Singh

Others also viewed

Explore content categories