object Construct

As usual the singleton is a class with behavior but CANNOT be instantiated.

Bag of Functions, with shared features. A Utils bin

In Scala you get a single instance of a class with some behavior. The object is stateful and can hold variables that are stuck onto it. For this to work, both defintions have to be in the same file.

Had to say this to myself several times to get my head around it.

object defines single instance of a class

Singleton has not parameters for the constructor.

Companion Objects

Very common in Scala. Gives you a way to build a class with static and instance methods.

Class has instance methods and fields. Object (Companion Object) has the static methods. Unlike other paradigms, in Scala the class and instance functionality is separated. The object’s functionality is not in the scope of the class, therefore calls from class have to the begin with the name of the object:

class School {
  val name = School.generate_name() // Call made to object.
  private var students = 0
  def add_student(child: Person) {
    students += 1

object School {
  private def generate_name() ={
    //build name