Scala Singleton and Companion Object
Scala Singleton Object
Singleton object is an object which is declared by using object keyword instead by class. No object is required to call methods declared inside singleton object.
In scala, there is no static concept. So scala creates a singleton object to provide entry point for your program execution.
Scala Singleton Object Example
Hello, This is Singleton Object
Scala Companion Object
In scala, 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.
Scala Companion Object Example
Hello, this is Companion Class. And this is Companion Object.
Next TopicScala Case Classes and Case Object