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
}
If you make any variable as final, you cannot change the value of final variable(It will be constant).
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.
Final variables If a variable is declared with the final keyword, its value cannot be changed once initialized.
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.
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
}
final
definition per the Scala Specification (emphasis mine):
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With