the blog for developers

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.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

Actually you can write “class Foo(private val bar : Bar)”.

2. How to have private fields

more concisely:

class Foo(private val bar: Bar) {

}

Tuomas

The second case is shorter with

class Foo(private val bar: Bar)

or just

class Foo(bar: Bar)

>To add getBar and setBar but not a public >field you need to:
>
>class Foo(aBar:Bar) {
> @BeanProperty
> private val bar = aBar
>}

For the setter to work I think you should define bar as a var, not val?

private var bar = aBar

Paul Phillips

Since it took me a long time to figure out it was even possible back in the day, how to have a private primary constructor:

class Foo private (x: Int) { }

[...] Code Monkeyism: Top 5 Things to Know About Constructors in Scala (tags: scala) [...]

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType