Mojo
Mojo

Reputation: 1202

Make An Empty Constructor for Scala Class

If I have a case class like this:

case class Foo(s : String, k : Int)

How do I specify an empty constructor for this? The reason I need this is because I want to pass this class value to a Java API which requires that class has an empty constructor. Do I specify default values for this? I think I have to, I have no choice.

Upvotes: 1

Views: 1021

Answers (1)

Tim
Tim

Reputation: 27421

You should be able to add a new constructor to the case class:

case class Foo(s : String, k : Int) {
  def this() = this("default", 0)
}

val foo = new Foo()

Thanks to Mario Galic for checking that this does create the appropriate constructor in Java.

Upvotes: 5

Related Questions