Alan Coromano
Alan Coromano

Reputation: 26008

Adding a custom logic to a Scala's constructor

Suppose I have a class

  class MyClass(a: Int, b: String) {
   //....
  }

If I want to add some custom logic (code) to this constructor, how do I do that?

Upvotes: 4

Views: 1524

Answers (1)

stew
stew

Reputation: 11366

class MyClass(a: Int, b: String) {
   // this is the constructor right here
   println("Hi, i'm the constructor")

   def imAMethod = 1

   println("Hi, I'm also part of the constructor down here, the whole class body is")
}

Upvotes: 17

Related Questions