Fields

Class instances keep state in their fields, which can be immutable or mutable. Immutable data is always encouraged in Scala, but there are scenarios where you absolutely need mutation.

Class fields can be explicitly or implicitly typed. In the latter case, the compiler infers the type of the field based on the value assigned to it.

Furthermore, the Scala compiler generates getters and setters (when necessary) for fields, which reduces the amount of code we need to write and maintain.

Read Understanding Classes and its Fields in Scala by Mohamed Sanualla to learn more about fields.

Test your knowledge below to make sure you understand Fields.