26

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)
arshajii
  • 127,459
  • 24
  • 238
  • 287
user2456976
  • 1,248
  • 2
  • 11
  • 19
  • Check out this question: http://stackoverflow.com/questions/15639078/scala-class-constructor-parameters – danielkza Sep 04 '13 at 21:27
  • This has a better explanation: http://stackoverflow.com/questions/14694712/do-scala-constructor-parameters-default-to-private-val – lcn Feb 13 '14 at 20:06

2 Answers2

29

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.

Travis Brown
  • 138,631
  • 12
  • 375
  • 680
  • 1
    Are you saying that in situations such as the example with `def fullName` there is no difference between a **`private val`** and a keyword-less parameter? – RussAbbott Oct 28 '13 at 16:46
  • @TravisBrown In 2.13.2 both `class Person(private[this] val firstName: String)` and `class Person(firstName: String)` generate field `private[this] val firstName: String` https://stackoverflow.com/a/62686327/5249621 – Dmytro Mitin Jul 01 '20 at 23:19
1

as a supplement, if your class is a case class, all of the constructor parameters will be automatically public fields.

The compiler will complain about the private keyword if it exists, and for the parameters without val/var, no matter they are used or not in any defs, there will be public fields generated for them.

Ambling
  • 446
  • 5
  • 12