Scala Tutorial - Learn How To Create And Use Companion Objects

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:15 pm

Overview

In this tutorial, we will learn how to create Companion Object for a given class. In turn, you will use the companion object to create instances for that particular class without having to make use of the new keyword.

Steps

1. How to define a simple class to represent a Donut object

Let's start with defining a simple class which will represent a Donut object with name and productCode properties. This example is a review from the previous tutorial on How To Create Classes And Objects In Scala.

 

Similar to the previous tutorial, let's also have a print() function which will print the name and productCode for a given donut object. If you are not familiar with creating functions in Scala, you can refer back to the Functions Tutorials.


println("Step 1: How to define a simple class to represent a Donut object")
class Donut(name: String, productCode: Long){

  def print = println(s"Donut name = $name, productCode = $productCode")

}

 

2. How to declare a companion object for the Donut class

A Companion Object is defined using the object keyword and the name of the object should be identical to the class name.

 

In addition, the companion object should define an apply() method which will be responsible for instantiating an instance of the given class.

 

Since the Donut class from Step 1 requires name and productCode properties, we are also providing similar input parameters to the apply() method as shown below.


println("\nStep 2: How to declare a companion object for the Donut class")
object Donut {

 def apply(name: String, productCode: Long): Donut = {
   new Donut(name, productCode)
 }

}

 

3. How to create instances of the Donut class using the companion object

With the companion object defined in Step 2 above, you can now create instances of the Donut class without having to use the new keyword.

 


println("\nStep 3: How to create instances of the Donut class using the companion object")
val glazedDonut = Donut("Glazed Donut", 1111)
val vanillaDonut = Donut("Vanilla Donut", 2222)

NOTE:

  • Sure you could argue that the companion object is not really adding any special value other than not having to use the new keyword.
  • However, in the next tutorial, I will show you how the apply() method of the companion object can be used as a factory for your class.

4. How to call the print function for each of the donut object

Next, let's call the print() function from our donut objects as shown below.


println("\nStep 4: How to call function on each Donut object")
glazedDonut.print
vanillaDonut.print

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to call the print function for each of the Donut object
Donut name = Glazed Donut, productCode = 1111
Donut name = Vanilla Donut, productCode = 2222

This concludes our tutorial on Learn How To Create And Use Companion Object and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to define a simple class to represent a Donut object
  • How to declare a companion object for the Donut class
  • How to create instances of the Donut class using the companion object
  • How to call the print function for each of the donut object

Tip

  • In upcoming tutorials, we will go over case classes which is a feature of Scala that will help you reduce boiler plate code by automatically generating accessors functions, hashCode and equals() methods.
  • What is more, case classes will also create companion objects for you!

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use Companion Objects as a factory.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on