Top 5 Things to Know About Constructors in Scala

I've been toying with Scala for some months now, one thing I've struggled with coming from Java are constructors in Scala. They are comparable to Java, but the syntax is different.

scala
Creative Commons License credit: .Paolo.

To help get you going faster in Scala, the top 5 things to know about constructors. Here we go:

  1. How to do constructors with a parameter
    public class Foo() {
       public Bar bar;
       
       public Foo(Bar bar) {
           this.bar = bar;
       }
    }
    

    Looks in Scala like this:

    class Foo(val bar:Bar)
    

    In this case val creates an immutable final public field, using var would create a mutable public field.

  2. How to have private fields
    public class Foo() {
       private final Bar bar;
       
       public Foo(Bar bar) {
           this.bar = bar;
       }
    }
    

    Looks in Scala like this:

    class Foo(private val bar: Bar)
    

    Update: Changed due to comments. Thanks for the commentors to point this out

    Private fields are not as necessary as in Java, you can have public fields for attributes and change them to a method (def) later - without changing your clients.

  3. How to use super() ?
    public class Foo() extends SuperFoo {
       public Foo(Bar bar) { 
          super(bar);
       }
    }
    

    Looks in Scala like this:

    class Foo(bar:Bar) extends SuperFoo(bar) 
    
  4. How to have more than one constructor?
    public class Foo {
        public Bar bar;
    	
        public Foo() { 
           this(new Bar()); 
        }
        
        public Foo(Bar bar) {
    	   this. bar = bar; 
        }
    }
    

    Looks in Scala like this:

    class Foo(val bar:Bar) {
      def this() = this(new Bar)
    }
    
  5. Secondary constructors like this() need to delegate to another constructor to work (Thanks @Synesso).

  6. How to get bean style setters and getters?
     public class Foo() {
       private Bar bar;
       
       public Foo(Bar bar) {
           this.bar = bar;
       }
       
       public Bar getBar() { 
          return bar; 
       }
       public void setBar(Bar bar) { 
          this.bar = bar;
       }
    }
    

    Looks in Scala like this:

    class Foo(@BeanProperty var bar:Bar)
    

    The attribute bar will still be public, which is not a big issue (see above) in Scala. But @BeanProperty helps when working with Java libraries and you need Bean conventions for the libraries to work.

    To add getBar and setBar but not a public field you need to:

    class Foo(aBar:Bar) {
        @BeanProperty
        private var bar = aBar
    }
    

Update: Changed to var, thanks to @eivindw.

Hope this helps you, if you have something to add, leave a comment. Should you struggle with the limited this() syntax (only one expression), then perhaps your constructors are doing too much. Consider the factory or better builder pattern instead.

Additional tip: use @Serializable to make your Scala classes serializable.

Nice coding in #Scala.