简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM