Do scala constructor parameters default to private val?

bar: Int

This is barely a constructor parameter. If this variable is not used anywhere except the constructor, it remains there. No field is generated. Otherwise private val bar field is created and value of bar parameter is assigned to it. No getter is created.

private val bar: Int

Such declaration of parameter will create private val bar field with private getter. This behavior is the same as above no matter if the parameter was used beside the constructor (e.g. in toString() or not).

val bar: Int

Same as above but Scala-like getter is public

bar: Int in case classes

When case classes are involved, by default each parameter has val modifier.

Leave a Comment

tech