×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala singleton and companion objects

By IncludeHelp Last updated : October 26, 2024

Scala being an object-oriented programming language has the capability to create an object that can define its members without a class.

Scala Singleton Objects

A singleton object has no class associated with it i.e. It exists without any class. It is defined using the object keyword not the class keyword and is an instance, not a blueprint hence it does not require an external call to execute its methods.

Why Singleton Object?

Every program needs a point from where the execution starts. In OOPS classes need objects to get executed. But the main() method needs to be executed first to call other members of the class.

For executing the main() method in scala many object-oriented programming languages use the static keyword but it scala programming language there is no static keyword. That is why in scala we use a singleton object that defines the main method.

Creating Singleton Objects

An object that can exist without a class is a singleton object. It objects keyword is used to define it.

The syntax to create singleton object is:

object singleton_objname {
  // object code , member functions and fields. 
}

Features of Singleton Object

  • Created using object keyword.
  • Members are global i.e can be called from anywhere in the program.
  • Creation of instance is not possible in case of singleton objects.
  • Inheritance is allowed, i.e. it can extend class and traits.
  • To excess members of Singleton object, we will use the singleton object's name dot members name.
    For example,
    Singleton_objname.member;

Example of Singleton Object

object FindSum {
  var a = 56
  var b = 21

  def sum(): Int = {
    a + b
  }
}

object Main { 
  def print(): Unit = { 
    println("The sum is: " + FindSum.sum())
  }

  def main(args: Array[String]): Unit = { 
    print()
  } 
}

Output

The sum is : 77

Explanation

This program is to illustrate the use of singleton objects in Scala. The program is to find the sum of given numbers and return it. There are 2 objects findsum and Main. The findsum object calculates the sum of the two numbers with the sum method. And the Main object has two methods print() and main(). The print method, prints the value that is returned by sum() method.

Scala Companion Object

If a class and a singleton object have the same name. Then, the class is called a companion class and singleton object is called a singleton object.

Both the class and object are defined in the same program file.

Example of Companion Object

Program to show implementation of companion object:

class companion{  
    var a = 23;
    var b = 78;
    def sum(){
        println("The sum is: "+ (a+b));
    }
}  

object companion{  
    def main(args:Array[String]){  
        new companion().sum(); 
    }  
}  

Output

The sum is: 101

Explanation

This program is to illustrate the concept of a companion object. Here, the program is used to find the sum of the given integer. To calculate the sum we have a method sum in companion class. We will call this method from the companion object using the dot operator.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.