Scala Object – Scala Singleton & Scala Companion Object

1. Scala Object – Objective

In our last Scala Programming Tutorial, we talked about Scala Annotations. Today, we will discuss Scala Object and different types of Objects in Scala. Moreover, we sill see Scala singleton object with example and Scala companion objects with an example.

So, let’s start Scala Singleton & Scala Companion Object.

Scala Object - Scala Singleton & Scala Companion Object

Scala Object – Scala Singleton & Scala Companion Object

2. Scala Singleton Objects

In Scala, an object is a class with exactly one instance. Like a lazy val, it creates lazily when we reference it. It is a value, and as a top-level value, it is a Scala singleton. To define an object, we use the keyword ‘object’:

scala> object Box
defined object Box

The methods we declare inside Scala singleton object are globally accessible, we don’t need an object for this. We can import them from anywhere in the program. And since there is no idea of ‘static’ in Scala, we must provide a point of entry for the program to execute. Scala singleton object makes for this. Without such an object, the code compiles but produces no output.
Let’s Learn Scala Case Class and How to Create Scala Object
Consider a package:

package logging
object Logger{
def info(message:String):Unit=println("Info: "+message)
Now consider another package:
class Project(name:String,days:Int)
class Test{
val project1=new Project("Book writing",365)
val project2=new Project("Revision",30)

With the import statement, we can use the method info() without a problem.
Scala singleton object can also extend traits and classes.

3. Scala Singleton Example

scala> object SingletonObject{
| def greet(){
| println("Hello")
| }
| }
defined object SingletonObject
scala> SingletonObject.greet()

As you can see, we don’t need an object to call the method greet() of this singleton object.
Do You Know Why Scala Called Object Oriented Programming 

Scala Quiz

4. Scala Companion Object

Coming from  singleton objects, we now discuss companion objects. A Scala companion object is an object with the same name as a class. We can call it the object’s companion class. The companion class-object pair is to be in a single source file. Either member of the pair can access its companion’s private members. Let’s take an example.

scala> class CompanionClass{
| def greet(){
| println("Hello")
| }
| }
defined class CompanionClass
scala> object CompanionObject{
| def main(args:Array[String]){
| new CompanionClass().greet()
| println("Companion object")
| }
| }
defined object CompanionObject

So, this was all about Scala Object Tutorial. Hope you like our explanation.

5. Conclusion

Hence, after reading this Scala Object Tutorial, we got to know what is singleton object in Scala and how it provides an entry for program execution. In addition, we talked about Scala companion object, an object for a class with the same name. Furthermore, if have any doubt, feel free to ask in the comment section.
Related Topic- Scala Arrays and Multidimensional Arrays
For reference

1 Response

  1. Vishal Kadam says:

    what is used of Scala Singleton & Scala Companion Object?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.