WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Basic · question 15 of 100

Explain the role of companion objects in Scala and how they relate to static methods in Java.?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

In Scala, there’s no concept of ‘static‘ keyword like Java. Hence, when you need a functionality similar to static methods or static fields in Java, you use companion objects. A companion object is an object that has the same name as the class and is defined in the same source file as the class.

Here’s an example of a simple Scala class with a companion object:

class MyClass {
  def greet(): String = {
    s"Hello, ${MyClass.defaultName}"
  }
}

object MyClass {
  private val defaultName: String = "World"
  
  def createDefault(): MyClass = {
    new MyClass()
  }
}

In this example, a ‘MyClass‘ class has a ‘greet‘ method, which uses the ‘defaultName‘ from the companion object. The companion object also has a ‘createDefault‘ method, which can be used to create an instance of ‘MyClass‘.

The companion object in Scala is similar to Java’s static methods in several ways:

1. Singleton: A companion object is a singleton, meaning there’s only one instance of this object in the entire program. This makes it a convenient place to store global states or define utility methods, just like Java’s static methods.

2. Access to private members: In Scala, a companion object and its corresponding class can access each other’s private members (fields and methods). This is a useful feature when you need to access some sensitive members without exposing them to the outside world, similarly to Java’s static methods.

3. Factory methods: One popular use case of companion objects is implementing factory methods, which create instances of the corresponding class, potentially with some specific configuration or logic. In such cases, you’d usually use the ‘apply‘ method, which allows you to instantiate objects without using the ‘new‘ keyword.

Here’s the previous example with ‘apply‘ method added:

class MyClass {
  def greet(): String = {
    s"Hello, ${MyClass.defaultName}"
  }
}

object MyClass {
  private val defaultName: String = "World"
  
  def apply(): MyClass = {
    new MyClass()
  }

  def createDefault(): MyClass = {
    new MyClass()
  }
}

val myInstance = MyClass() // creates a new instance using the apply method

Although companion objects have similarities to Java’s static methods, there are differences too:

1. Companion objects are instances: Unlike Java’s static methods, which are associated with the class itself and not instances, companion objects in Scala are instances of a separate class. This means that they can implement traits or inherit from classes, allowing more flexibility when organizing your code.

2. Instance methods in companion objects: Since companion objects are instances, you can define instance methods in them, which can’t be done in a static context in Java.

To summarize, companion objects in Scala provide a place to contain functionality similar to Java’s static methods. They are singletons that can access private members of their corresponding class and vice versa, and they are a common way to implement factory methods and group utility methods that belong to a particular class.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics