This is addressed explicitly in the specification, and they are automatically final:
Members of final classes or objects are implicitly also final, so
thefinal
modifier is generally redundant for them, too. Note, however, that
constant value definitions (§4.1) do require an explicitfinal
modifier, even if
they are defined in a final class or object.
Your final
-less example compiles without errors (or warnings) with 2.10-M7, so I’d assume that there’s a problem with the @switch
checking in earlier versions, and that the members are in fact final.
Update: Actually this is more curious than I expected—if we compile the following with either 2.9.2 or 2.10-M7:
object NonFinal {
val a = 0
}
object Final {
final val a = 0
}
javap
does show a difference:
public final class NonFinal$ implements scala.ScalaObject {
public static final NonFinal$ MODULE$;
public static {};
public int a();
}
public final class Final$ implements scala.ScalaObject {
public static final Final$ MODULE$;
public static {};
public final int a();
}
You see the same thing even if the right-hand side of the value definitions isn’t a constant expression.
So I’ll leave my answer, but it’s not conclusive.