Search code examples
scalacase-class

Are case class constructor parameters public val fields by default?


With reference to the official Scala Documentation: case-classes.html
"Case class constructor parameters are public val fields by default"

However, the decompilation of .scala

case class A(i: Int)

shows the below of Java code

private final int i; // private, not public
public int i(){ return i; }

Does the statement in case-classes.html mean:
"Case class constructor parameters are private val fields by default."
However, an automatically generated getter makes it public.


Solution

  • Yes, constructor parameters for case classes are public vals from a language perspective. However a val is immutable, so it must be implemented as a private value in the class with a public getter but no setter.