Tech and Media Labs
This site uses cookies to improve the user experience.




Scala Classes

Jakob Jenkov
Last update: 2015-05-23

Scala is, among other things, an object oriented language. This means that you can define classes in Scala, and instantiate objects of these classes. I expect that you know the basics of object oriented programming, when reading this text.

A Scala class is a template for Scala objects. That means, that a class defines what information objects of that class holds, and what behaviour (methods) it exposes. A class can contain information about:

  • Fields
  • Constructors
  • Methods
  • Superclasses (inheritance)
  • Interfaces implemented by the class
  • etc.

In this text I will focus on just the fields, constructors and methods. The other aspects of Scala classes will be covered in separate texts.

The Basic Class Definition

Here is a simple class definition in Scala:

class MyClass {

}

This class is not very interesting. I will add some more to it throughout this text.

Fields

A field is a variable that is accessible inside the whole object. This is contrast to local variables, which are only accessible inside the method in which they are declared. Here is a simple field declaration:

class MyClass {
  var myField : Int = 0;
}

This declaration defines a field of type Int and initializes it to the value 0.

Scala's type inference can figure out the type of a variable, based on the value assigned to it. Therefore, you could actually omit the type in the field declaration above, like this:

class MyClass {
  var myField = 0;
}

Since 0 is by default assumed to be an Int, the Scala compiler can infer the type of the myField based on the 0 assigned to it.

Constructors

In scala constructors are declared like this:

class MyClass {
    var myField : Int = 0;

    
        def this(value : Int) = {
        this();
        this.myField = value;
        }
    
  }

This example defines a constructor which takes a single parameter, and assigns its value to the field myField.

Notice the = between the parameters and the constructor body (the { ). This equals sign must be present in constructors.

Also notice the explicit call to the no-arg constructor, this();. All constructors except the no-arg constructor must always call another constructor in the beginning of its body.

Methods

In Scala methods in a class are defined like this:

class MyClass {
  var myField = 0;

  def getMyField() : Int = {
    return this.myField;
  }

}

The above example defines a method called getMyField. The return type, Int, is declared after the method name. Inside the { and } the method body is declared. The method currently just returns the myField field. Notice the = sign between the Int and {. Methods that return a value should have this equals sign there.

I will cover methods and functions in more detail in a separate text. Here I have just shown you how to declare a method inside a class.

Here is a method that doesn't return anything, but instead modifies the internal state (field) of the object. Notice how this addToMyField() method does not have the equals sign, and no return type specified.

    class MyClass {
      var myField = 0;

      def getMyField() : Int = {
        return this.myField;
      }

     
         def addToMyField(value : Int) {
         this.myField += value;
         }
     
    }

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC