29 Classes

  • classes can be defined with minimal amount of code
  • the class body, is also the default constructor’s implementation
  • automatic getters are generated for the class parameters defined using val   e.g.  
class Person(val name:String) //generates a private `name` variable, and a getter with the same name   
  • automatic getters and setters are generated for class parameters defined using var e.g.
class Person(var name:String) //generates a private name variable, a getter and a setter with the same name 
  • Important Note: the private variable with the same name as the automatic getter and setter exists only in byte code. It’s not possible to recreate it using explicit scala getters and setters (having a method and a variable of the same name violates the uniform access principle, and scala’s scoping rules). To create explicit getters and setters - the private variable must have a different name, some like to add an _ before it to designate it is private and local and avoid naming conflicts with public methods
  • everything is public by default unless explicity declared otherwise

Code editor is using Scalakata.com written by Guillaume Massé