SCALA : Handling visibility of constructor fields

Table of contents
Reading Time: < 1 minute

Constructor fields are parameters of class . In scala , we can define the visibility of parameters , visibility of parameters can be defined by “val” or “var” or without “val” or “var” .

For example : class Knoldus(val name:String)

Here, “val” defines the visibility of constructor field.

With val field

As we know that “val” is immutator so mutator method is not generated .In case of “val” field , we can not reassign value (scala>”XYZ” ) .

With var field

In this case , we accessed the value and reassigned (mutated) the value because “var” is mutator .

Without val or val field

In this case , visibility of parameters are very restricted that why we can not access or set the value

Note : In “case class” , constructor fields are val by default .Case class rules are differ from above rules .