0 votes
in Scala Constructs by
Explain the scope provided for variables in Scala.

1 Answer

0 votes
by

There are three different scopes depending upon their use. Namely:

Fields:

Fields are variables declared inside an object and they can be accessed anywhere inside the program depending upon the access modifiers. Fields can be declared using var as well as val.

Method Parameters:

Method parameters are strictly Immutable. Method parameters are mainly used to Pass values to the methods. These are accessed inside a method, but it is possible to access them from outside the method provided by a Reference.

Local Variables:

Local variables are declared inside a method and they are accessible only inside the method. They can be accessed if you return them from the method.

 

...