Scala Language Objects


Example

Whereas Classes are more like blueprints, Objects are static (i.e. already instantiated):

object Dog {
    def bark: String = "Raf"
}

Dog.bark() // yields "Raf"

They are often used as a companion to a class, they allow you to write:

class Dog(val name: String) {

}

object Dog {
    def apply(name: String): Dog = new Dog(name)
}

val dog = Dog("Barky") // Object
val dog = new Dog("Barky") // Class