Geek Culture
Published in

Geek Culture

Should you use singleton objects in Scala?

Understanding singleton objects in Scala

Photo by Volodymyr Hryshchenko on Unsplash

Hey Folks, happy weekend!

If you are familiar with object-oriented programming you must be familiar with the concept of a class, widely defined as

A class is a blueprint that defines the variables and the methods common to all objects of a certain kind.

Image by author

In this article, we will discuss an interesting type of class in scala — singleton objects. Also, don’t worry if you are not familiar with the concepts of object-oriented programming in detail, you will get an understanding along the way!

What are singleton objects?

So, let’s begin with an understanding of singleton objects:

An singleton object is a class that has exactly one instance.

Now you must be thinking(at least I was when I first read it):

Wait what? So, object is an class, but aren’t classes supposed to have objects? What in the fresh hell is this and why the need for such a thing?

Well, yes! These objects are a special type of class that has a single instance and can be utilized across the package — Meaning, that all the methods and variables in this class can be accessed across the package. (If you are aware of static keywords, you can relate the same concepts here)

Let’s see what a singleton object looks like:

// ArticleUtility is a singleton object hereobject ArticleUtility {
var platform = "Medium"

def publish(article: String): Unit = {
println(article)
}
}

The definition looks like how exactly you will define a class(except for the keyword difference, of course), pretty simple, right?

ArticleUtility is a singleton object here having a variable platform and a method publish. Now anywhere I want to access this attribute or method, I can simply do

import ArticleUtility.platform
import ArticleUtility.publish
publish("Should you use singleton objects in scala?")println(s"This article will be published on $platform")

See, we were able to access the methods and attributes without creating any instance. Question to ask here is,

If it is actually a class, how is it even working? When is it being instantiated?

The answer — It is created lazily when it is referenced. I won’t go into details about how, but it is good to know how an object works.

So, having defined singleton objects, what are the use cases where singleton objects can be used?

  • The main method in scala is always written in a singleton object
  • Good to use for common utilities
  • Constants

Defined above are some of the use cases of objects, it seems pretty cool to write great modularized code, but remember — with great power comes great responsibility!

Things to note while using singleton objects

Remember it has a single instance, so wherever to say, platform is accessed, the same variable will be accessed — Meaning if you import and modify platform in a file, it will be modified across the complete module.

It has both its benefits and cons. Benefits being, cleaner code and cons being, when you start using them as global variables in generic methods and we all know global variables have such a bad reputation!

All in all, singleton objects really support the object-oriented nature of Scala. It really helps in keeping code modularized but it is really important to note how they work and behave.

Well, that’s it. If you are interested in reading more about singleton objects — the next step from here is Companion objects

Do let me know in the comments if you want me to write about Companion objects or how you have used singleton objects in Scala. Lastly, if you like this article, please show your support with clap(s) :)

Happy coding,
JD

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store