Scala – Companion Object

In the previous chapter, we have learnt about the Singleton Object. Now, let’s see study about companion object.

When you have a class with same name as singleton object, it is called companion class and the singleton object is called companion object. The companion class and its companion object both must be defined in the same source file.

Define nonstatic (instance) members in your class, and define members that you want to appear as “static” members in an object that has the same name as the class.

Scala - Singleton Object
Scala - Common code in package object
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

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