Posts

Showing posts with the label Class

Visbility of Class members in Scala

Image
Scala programming model controls the visibility of class constructor arguments. Based on the declaration of the fields as val, var or private, the compiler generates the accessors and mutators accordingly. The following matrix explains how the compiler generates the accessors and mutators of the class internally based on the declaration of the fields. Val Fields Assume if the class constructor is declared with Val fields, the value of the fields can be accessed but not changed. In general, the val declaration is meant for immutable fields. Thus Scala does not generate the mutator for the val fields.  class Person(val firstName: String, val lastName: String) { override def toString = s"$firstName $lastName" def printFullName{ println(this)} } object Test extends App{ var p = new Person("Murugan", "Shiva") p.printFullName p.firstName = "Ganesh" // Compiler error: reassignment to val p.printFullName } Var Fields The var fields...

Popular posts from this blog

Trait, an interface in Scala

Singleton class made easy in Scala

Quickstart with Angular 2/4