Singleton and Companion Objects in Scala.

Lavlesh Singh
Analytics Vidhya
Published in
3 min readNov 9, 2020

--

Scala is an object-oriented programming language, that defines singleton and companion objects to make it’s functioning enjoyable ..

Scala is more object oriented language than Java so, Scala does not contain any concept of static keyword. Instead of static keyword Scala has singleton object. A Singleton object is an object which defines a single object without class. A singleton object provides an entry point to your program execution. If you do not create a singleton object in your program, then your code compile successfully but does not give output. So you required a singleton object to get the output of your program. A singleton object is created by using object keyword.

Photo by C D-X on Unsplash

Singleton objects in Scala:-

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

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.

Important point :-

Scala supports static members, but not in the same manner as Java. Scala provides an alternative to this called Singleton Objects. Singleton objects are similar to a normal class, except they can not be instantiated using the new keyword.

Important points about singleton object:-

1- Created using object keyword.
2- Creation of instance is not possible in case of singleton objects.
3- Inheritance is allowed, i.e. it can extend class and traits.
4- To excess members of Singleton object, we will use the singleton object’s name dot members name.
5- The method in the singleton object is globally accessible.
6- You are not allowed to pass parameter in the primary constructor of singleton object.
7- In Scala, a main method is always present in singleton object.

Why Singleton Object in Scala:-

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.

Situation where you can use :-

Suppose you have a method which identifies if the password entered is a weak or strong password, then you can create this method in a Singleton Object and share it with your team members to use it wherever required.

Syntax:-

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

Example:-

object summing{
var a = 56
var b = 21
def sum(): Int ={
return a+b;
}
}
object Main
{
def print(){
printf("The sum is : "+ summing.sum());
}
def main(args: Array[String])
{
print();
}
}

Output

The sum is : 77

Companion object in Scala:-

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.

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();
}
}

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.

Thanks ! reader for referring my post on Scala for more Interesting topics, you can check my previous post ..

--

--