Scala Constructor Parameters

user2456976 picture user2456976 · Sep 4, 2013 · Viewed 11.7k times · Source

What is the difference between a private var constructor parameter and a constructor parameter without val/var? Are they same in terms of scope/visibility?

Ex:

class Person(private var firstName:String, lastName:String)

Answer

Travis Brown picture Travis Brown · Sep 4, 2013

Yes, there are two important differences. First for the easy one: constructor parameters without the var or val keywords are not mutable variables—their values can't be changed in the body of the class.

Even if we restrict ourselves to the val keyword, though, there's still a difference between private val and keyword-less parameters. Consider the following:

class Person(private val firstName: String, lastName: String)

If we look at the compiled class with javap -v Person, we'll see that it only has one field, for firstName. lastName is just a constructor parameter, which means it may be garbage-collected after the class is initialized, etc.

The compiler is smart enough to know when the value of lastName will be needed after initialization, and it will create a field for it in that case. Consider the following variation:

class Person(private val firstName: String, lastName: String) {
  def fullName = firstName + " " + lastName
}

The compiler can tell that it may need the value of lastName later, and if we check javap again we'll see that the class has two fields (note that if we'd defined fullName as a val instead of a def, it'd only have one field).

Lastly, note that if we make firstName object-private instead of class-private, it works exactly like a plain old keyword-less constructor parameter:

class Person(private[this] val firstName: String, lastName: String)

This works even with var instead of val:

class Person(private[this] var firstName: String, lastName: String)

Both of these classes will have no fields. See section 5.2 of the language specification for more details about object-private access.