Scala: Basic Class Creation

In this tutorial I will show you how to create your first Scala class and then use it. I am just beginning with Scala during the time of this writing. Review the Scala style guide.

So the first thing we want to do is determine what we want to create a class to represent. In this tutorial I am just going to play around and use Person. We will want to create a constructor, getters, setters, toString and then finally a method to combine some properties.

Create your class.

class Person {
}

We could have added variables to the Person declaration. But I thought I’d leave that out for now.

Create our private first and last name.

private var _firstName: String = null

When we set variables as private in the class they are not accessible from outside the class. Notice how the variable starts with “_” this is just one of the Scala naming conventions.

Create our constructor

/**
* @constructor Creates a person with first/last name
* @param firstName the persons first name
* @param lastName the persons last name
*/
def this(firstName: String, lastName: String) {
  this()
  _firstName = firstName
  _lastName = lastName
}

This is where we can set the first and last name when we instantiate our object.

Create a getter

def firstName = _firstName

Create a setter

def firstName_=(firstName: String) {
  _firstName = firstName
}

Override toString

override def toString = s"firstName = $firstName"

Notice how their is “s” before the string and we have $firstname there. That will reference the variable itself.

Create a Method

def fullName: String = {
  return s"$firstName $lastName"
}

This will just give you the full name of the person.

Putting it all together

package models

class Person {
  private var _firstName: String = null
  private var _lastName: String = null
  
  /**
   * @constructor Creates a person with first/last name
   * @param firstName the persons first name
   * @param lastName the persons last name
   */
  def this(firstName: String, lastName: String) {
    this()
    _firstName = firstName
    _lastName = lastName
  }
    
  //Getter
  def firstName = _firstName
  def lastName = _lastName
  
  //Setter
  def firstName_=(firstName: String) {
    _firstName = firstName
  }
  
  def lastName_=(lastName: String) {
    _lastName = lastName
  }
  
  def fullName: String = {
    return s"$firstName $lastName"
  }
  
  override def toString = s"firstName = $firstName, lastName = $lastName"
}

So what I have shown you above will get you started on creating your first class but you could make it alot cleaner with less code. it’s entirely up to you how you want to proceed and what you feel comfortable with.

package models

class PersonCondensed {
  var firstName:String = null
  var lastName:String = null
  
  /**
   * @constructor Creates a person with first/last name
   * @param firstName the persons first name
   * @param lastName the persons last name
   */
  def this(firstName: String, lastName: String) {
    this()
    this.firstName = firstName
    this.lastName = lastName
  }
  
  def fullName: String = {
    return s"$firstName $lastName"
  }
  
  override def toString = s"firstName = $firstName, lastName = $lastName"
}

Using our class

Here are the three different ways of calling our classes we did above.

import models.Person
import models.PersonCondensed

object Test {
  def main(args: Array[String]) {
    val person = new Person() 
    person.firstName_=("John")
    person.lastName_=("Smith")
    person.value=(234)
    
    println(person.fullName)
    println(person.toString())    
    
    val person2 = new Person("John", "Smith") 
    
    println(person2.fullName)
    println(person2.toString())
    
    val person3 = new PersonCondensed()
    person3.firstName=("John")
    person3.lastName=("Smith")
    
    println(person3.firstName)
    println(person3.lastName)
    println(person3.fullName)
    println(person3.toString())
  }
}