Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

SCALA Properties / classes

4.61/5 (6 votes)
16 Oct 2015CPOL3 min read 33.2K  
Scala properties/classes

In this post, we will be covering the very basics of how to create properties in our Scala classes, and also talk through how to create simple classes.

Constructors

Let’s start by creating a very simple class called “Person” that will have a single constructor that takes a string for firstname and a string for lastname.

The firstname and lastname should have getters but no setters.

In Scala, this is achieved as follows:

Java
class Person(val firstName: String, val lastName: String) { 

} 

Which we may use like this:

Java
object ClassesDemo {
  def main(args: Array[String]) =
  {
    val person = new Person("sacha","barber")
    val theirLastName = person.lastName
 
    System.out.print(s"Persons last name is $theirLastName")
 
    System.in.read()
 
    ()
  }
} 

See how we are using val, this means it is immutable, so we only expose a getter, no setter. If we wanted a getter and a setter, we would instead use var like this:

We can also control the access modifiers directly in the constructor as follows:

Java
class Person(val firstName: String, private var lastName: String) { 

} 

Which now causes a problem is we try and use the lastname field outside of the Person class:

image

Secondary Constructors

As in .NET, it is possible to have multiple constructors. However, the rule in Scala is that any NON primary constructor MUST ALWAYS call the primary constructor.

Here is an example using our simple Person class, which has a secondary constructor:

Java
///Primary constructor
class Person(var firstName: String, var lastName: String) {
 
  //secondary constructor
  def this(firstName: String) {
    this(firstName, "")
  }
 
  override def toString: String = {
    return s"firstname: $firstName, lastname: $lastName"
  }
}

Which we may use like this:

Java
object ClassesDemo {
  def main(args: Array[String]) =
  { 
    val person1 = new Person("sacha","barber")
    val person2 = new Person("ryan")

    System.out.print(s"Person1 is $person1\r\n")
    System.out.print(s"Person2 is $person2\r\n")
 
    System.in.read()
    ()
  }
} 

So that talks about how you might write constructors.

Get/Set

Another thing we may want to add to our classes (outside of constructors) is properties. Here is our (by now famous) person class, rewritten to include a separate age property.

Java
//Primary constructor
class Person(var firstName: String, var lastName: String) {
 
  // Private age variable, renamed to _age 
  private var _age = 0
  var name = ""
 
  // Getter 
  def age = _age
 
  // Setter 
  def age_= (value:Int):Unit = _age = value 
 
  override def toString: String = {
    return s"firstname: $firstName, lastname: $lastName, age: $age"
  }
}

Which we can use as follows:

Wait a minute, aren’t we meant to be calling a method when we use the age setter? In Scala, parentheses are usually optional. The age setter line could just as easily been written as:

Java
person1.age =(99) 
// OR 
person1.age_=(99) 

Tooling Compatability

Scala is a JVM language but is it's not Java, and as such sometimes you may need to do a bit more work if you want to interop with Java correctly. Bean properties may be one area where extra work is required.

The reason for this is that in Java Beans, there is an expectation that there are methods call getXXX() and setXXX().

Mmm curious. Luckily in Scala, this is easy to fix, we just need to use a simple BeanProperty annotation.

Java
//Primary constructor
class Person(var firstName: String, var lastName: String) {
 
  @scala.beans.BeanProperty
  var age = 0 
 
  override def toString: String = {
    return s"firstname: $firstName, lastname: $lastName, age: $age"
  }
}

Which you may then use like this:

Java
val person1 = new Person("sacha","barber") 
person1.age = 12 
var theirAge = person1.getAge() 

Note you may also do this on the constructor which may help should you wish to expose constructor parameters as bean properties:

Java
class Person(
              @scala.beans.BeanProperty
              var firstName: String,
              @scala.beans.BeanProperty
              var lastName: String) {
}

Methods

Methods in Scala are simply defined as follows:

Java
def ReverseAString(inputString:String) : String = { 
    return inputString.reverse 
  } 

Where we specify that it is a method using the def keyword, and then a name for the method, and then any parameters followed by a colon and then a return type, then we supply the method body.

We then can call this method like this (assumes we added this method to our Person class)

Java
val person1 = new Person("sacha","barber") 
val reversed = person1.ReverseAString("The cat sat here") 

Static Classes / Static Methods

This is certainly one area where Scala and .NET differ. In Scala static methods, go into a companion object for the type.

I actually think the Scala way makes a lot more sense, when you think about. The PersonOBJECT” has this method. Anyway, here is a small example:

Java
//Companion object where Static things live
object Person
{
  def StaticDoubleIntMethodOnPersonType(input : Int) : Int = {
    return input * 2
  }
}
 
//Primary constructor
class Person(var firstName: String, var lastName: String) {
 
  override def toString: String = {
    return s"firstname: $firstName, lastname: $lastName"
  }
} 

Which you could use like this:

Java
val doubled = Person.StaticDoubleIntMethodOnPersonType(10) 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)