Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Final variables in scala are allowed to change values

Tags:

scala

Why Final variables in Scala are allowed to change values. As per my understanding once declared final, they shouldn't be allowed to change.

class foo()
{
  final var name = "abc"
  name = "xyz" // why this is allowed
}
like image 395
jack Avatar asked Feb 14 '17 07:02

jack


People also ask

Can final variable value be changed?

If you make any variable as final, you cannot change the value of final variable(It will be constant).

What happens when you try to change a final variable?

We can assign the value in the final variable declaration or in the class constructor. In case we try to change the final variable value later, the compiler will throw an error.

What happens if the variable is final?

Final variables If a variable is declared with the final keyword, its value cannot be changed once initialized.

What does final do in Scala?

In Scala, Final is a keyword and used to impose restriction on super class or parent class through various ways. We can use final keyword along with variables, methods and classes. Scala Final Variable: Scale final variable initialized only once while declared and used as constant throughout the program.


2 Answers

The final keyword does not bear the same meaning it does in Java.

In Java it can both mean that a class or method cannot be extended or overridden and that a reference is immutable.

In Scala final only bears the first meaning, while to have an immutable reference you ought to use the val keyword.

class Foo {
  val name: String = "abc"
  // name = "xyz" // would be a compilation error

  final var surname: String = "def"
  surname = "uvw" // ok
}

class Bar extends Foo {
  override val name: String = "xyz"
  override var surname: String = "rst" // compilation error
}
like image 173
stefanobaghino Avatar answered Oct 18 '22 05:10

stefanobaghino


final definition per the Scala Specification (emphasis mine):

5.2.6 final

The final modifier applies to class member definitions and to class definitions. A final class member definition may not be overridden in subclasses. A final class may not be inherited by a template. final is redundant for object definitions. Members of final classes or objects are implicitly also final, so the final modifier is generally redundant for them, too. Note, however, that constant value definitions do require an explicit final modifier, even if they are defined in a final class or object. final may not be applied to incomplete members, and it may not be combined in one modifier list with sealed.

Since a val definition in Scala already means that the reference to it is immutable (unlike Java), you don't have to explicitly specify that when defining it. If you want the value to be inlined as a constant in the byte code, you can specify add the final modifier.

As per var, final here only means "may not be overridden in subclasses", but says nothing about the immutability of the variable:

scala> :pa
// Entering paste mode (ctrl-D to finish)

class Foo {
  final var name = "abc"
}

class Bar extends Foo {
  override var name = "yuval"
}

// Exiting paste mode, now interpreting.

<console>:16: error: overriding variable name in class Foo of type String;
 variable name cannot override final member
             override var name = "yuval"
like image 38
Yuval Itzchakov Avatar answered Oct 18 '22 04:10

Yuval Itzchakov