Scala what is the difference between defining a method in the class instead on the companion object

agusgambina

I have a doubt about what is the difference between this two pieces of code.

The obvious ones are that if the method is written in the class you must instantiate the class, while if it is in the companion object, you are not required to do that.

However, Are there also other differences? Which are the benefits and drawbacks?

define a method in the class

class Hello {
  def hello = println("Hello world")
}

object Hello {
  def main(args: Array[String]) {
    val instance = new Hello()
    instance.hello
  }
}

define a method in the companion object

class Hello {

}

object Hello {

  def hello = println("Hello world")

  def main(args: Array[String]) {
    this.hello
  }
}
radumanolescu

If you remember that Scala came from Java, it may make a bit more sense. In Java, there's only a class, and it has static methods, which do not depend on any fields in an instance (but can read static fields) and regular (non-static, instance-level) methods. The distinction is between "functionality that is common to SomeType but to no instance in particular", and "functionality that needs the state of a particular instance". The former are static methods in Java, and the latter are instance methods. In Scala, all the static parts should go in the object, with the instance-level val and def being in the class. For example:

object MyNumber {
    // This does not depend on any instance of MyNumber
    def add(c: Int, b: Int): Int = c + b
}
class MyNumber(a: Int) {
    // This depends on an instance of MyNumber
    def next: Int = a + 1
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the difference between a companion object and a singleton class in Scala (Guice)

Kotlin: Difference between object and companion object in a class

What are the differences between "object equality" created by the factory method of the companion object and case class?

Instantiating a class's subtype in the companion object's apply method in Scala

Difference between instantiating something from the class and from the companion object

difference between fun in companion object block and outside of class in kotlin?

what's the difference between val and const val in companion object?

Difference between object and class in Scala

Companion Object for Generic class | Scala

What's difference between two ways of defining method on React Class in ES6

What is the difference between a class and an object?

What is the difference between defining a method with the `where` clause vs omitting it in Julia?

Difference in Scala between abstracting class versus method

What is the difference between class.method and object.method in Java and what are its cons and pros

What is the difference between two ways of defining object methods in Javascript?

Scala - make method from List Companion Object

Scala companion object override method error

Differences between Anonymous, Singleton & Companion Object in Scala

What is the difference between a class and a type in Scala (and Java)?

scala case class put methods in companion object?

Any way to avoid defining a props method in an Akka companion object?

Unable to create companion class instance in companion object method

IntelliJ: Jump between companion object and class

what's the difference between property and class method?

what is the difference between ? and T in class and method signatures?

Difference between class object Method declaration React?

The difference between function, class, method, object and constructor?

difference between 'Class<?>' and 'Object' as argument of method

What is the difference between a method and a proc object?