Scala: Basic Class Creation

(Last Updated On: )

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.

  1. class Person {
  2. }

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.

  1. 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

  1. /**
  2. * @constructor Creates a person with first/last name
  3. * @param firstName the persons first name
  4. * @param lastName the persons last name
  5. */
  6. def this(firstName: String, lastName: String) {
  7. this()
  8. _firstName = firstName
  9. _lastName = lastName
  10. }

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

Create a getter

  1. def firstName = _firstName

Create a setter

  1. def firstName_=(firstName: String) {
  2. _firstName = firstName
  3. }

Override toString

  1. 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

  1. def fullName: String = {
  2. return s"$firstName $lastName"
  3. }

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

Putting it all together

  1. package models
  2.  
  3. class Person {
  4. private var _firstName: String = null
  5. private var _lastName: String = null
  6. /**
  7. * @constructor Creates a person with first/last name
  8. * @param firstName the persons first name
  9. * @param lastName the persons last name
  10. */
  11. def this(firstName: String, lastName: String) {
  12. this()
  13. _firstName = firstName
  14. _lastName = lastName
  15. }
  16. //Getter
  17. def firstName = _firstName
  18. def lastName = _lastName
  19. //Setter
  20. def firstName_=(firstName: String) {
  21. _firstName = firstName
  22. }
  23. def lastName_=(lastName: String) {
  24. _lastName = lastName
  25. }
  26. def fullName: String = {
  27. return s"$firstName $lastName"
  28. }
  29. override def toString = s"firstName = $firstName, lastName = $lastName"
  30. }

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.

  1. package models
  2.  
  3. class PersonCondensed {
  4. var firstName:String = null
  5. var lastName:String = null
  6. /**
  7. * @constructor Creates a person with first/last name
  8. * @param firstName the persons first name
  9. * @param lastName the persons last name
  10. */
  11. def this(firstName: String, lastName: String) {
  12. this()
  13. this.firstName = firstName
  14. this.lastName = lastName
  15. }
  16. def fullName: String = {
  17. return s"$firstName $lastName"
  18. }
  19. override def toString = s"firstName = $firstName, lastName = $lastName"
  20. }

Using our class

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

  1. import models.Person
  2. import models.PersonCondensed
  3.  
  4. object Test {
  5. def main(args: Array[String]) {
  6. val person = new Person()
  7. person.firstName_=("John")
  8. person.lastName_=("Smith")
  9. person.value=(234)
  10. println(person.fullName)
  11. println(person.toString())
  12. val person2 = new Person("John", "Smith")
  13. println(person2.fullName)
  14. println(person2.toString())
  15. val person3 = new PersonCondensed()
  16. person3.firstName=("John")
  17. person3.lastName=("Smith")
  18. println(person3.firstName)
  19. println(person3.lastName)
  20. println(person3.fullName)
  21. println(person3.toString())
  22. }
  23. }