104

I used to think that private val and private final val are same, until I saw section 4.1 in Scala Reference:

A constant value definition is of the form

final val x = e

where e is a constant expression (§6.24). The final modifier must be present and no type annotation may be given. References to the constant value x are themselves treated as constant expressions; in the generated code they are replaced by the definition’s right-hand side e.

And I have written a test:

class PrivateVal {
  private val privateVal = 0
  def testPrivateVal = privateVal
  private final val privateFinalVal = 1
  def testPrivateFinalVal = privateFinalVal
}

javap -c output:

Compiled from "PrivateVal.scala"
public class PrivateVal {
  public int testPrivateVal();
    Code:
       0: aload_0       
       1: invokespecial #19                 // Method privateVal:()I
       4: ireturn       

  public int testPrivateFinalVal();
    Code:
       0: iconst_1      
       1: ireturn       

  public PrivateVal();
    Code:
       0: aload_0       
       1: invokespecial #24                 // Method java/lang/Object."<init>":()V
       4: aload_0       
       5: iconst_0      
       6: putfield      #14                 // Field privateVal:I
       9: return
}

The byte code is just as Scala Reference said: private val is not private final val.

Why doesn't scalac just treat private val as private final val? Is there any underlying reason?

3
  • 29
    In other words: since a val is already immutable, why do we need the final keyword at all in Scala? Why can't the compiler treat all vals the same way as final vals?
    – Jesper
    Nov 16, 2012 at 8:00
  • Note that the private scope modifier has the same semantics as package private in Java. You may mean to say private[this]. Nov 17, 2012 at 6:59
  • 6
    @ConnorDoyle: As package private? I don't think so: private mean that it is only visible to instances of this class, private[this] only this instance - except for instances of the same class, private does not allow anyone (include from the same package) to access the value.
    – Make42
    Oct 31, 2016 at 21:03

2 Answers 2

84

So, this is just a guess, but it was a perennial annoyance in Java that final static variables with a literal on the right-hand side get inlined into bytecode as constants. That engenders a performance benefit sure, but it causes binary compatibility of the definition to break if the "constant" ever changed. When defining a final static variable whose value might need to change, Java programmers have to resort to hacks like initializing the value with a method or constructor.

A val in Scala is already final in the Java sense. It looks like Scala's designers are using the redundant modifier final to mean "permission to inline the constant value". So Scala programmers have complete control over this behavior without resorting to hacks: if they want an inlined constant, a value that should never change but is fast, they write "final val". if they want flexibility to change the value without breaking binary compatibility, just "val".

4
  • 9
    Yes, that's the reason for non-private vals, but private vals obviously can't be inlined in other classes and break compatibility in the same way. Nov 16, 2012 at 8:30
  • 3
    Is there any binary compatibility issue when I change private val to private final val?
    – Yang Bo
    Nov 16, 2012 at 8:35
  • 1
    @steve-waldman Excuse me, did you mean val in your second paragraph?
    – Yang Bo
    Nov 16, 2012 at 8:45
  • 1
    Here are the details on final static variables in Java regarding binary compatibility - docs.oracle.com/javase/specs/jls/se7/html/…
    – Eran Medan
    Jul 10, 2013 at 3:02
8

I think the confusion here arises from conflating immutability with the semantics of final. vals can be overridden in child classes and therefore can't be treated as final unless marked as such explicitly.

@Brian The REPL provides class scope at the line level. See:

scala> $iw.getClass.getPackage
res0: Package = package $line3

scala> private val x = 5
<console>:5: error: value x cannot be accessed in object $iw
  lazy val $result = `x`

scala> private val x = 5; println(x);
5
3
  • 2
    I am talking about private val. Can it be overriden?
    – Yang Bo
    Nov 17, 2012 at 13:40
  • No, private vals can't be overriden. You can redefine another private val with the same name in a subclass, but it's a completely different val that just happens to have the same name. (All references to the old one would still refer to the old one.)
    – aij
    Jan 21, 2014 at 2:46
  • 1
    it doesn't seem to just be this overriding behavior though, since I can make a final val (or even a final var) at the interpreter without being in the context of a class at all.
    – nairbv
    Feb 16, 2014 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.