Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reassignment to val while initializing in primary constructor

class Person(){
    val name : String 
    def this(n : String) {
      this()
      this.name = n
    }
  }

compile time error : reassignment to val

i am a newbie to scala and so far i learned how to use primary constructor and case classes for initialization of data members. I am just wandering, if there is a way to initialize val data member inside this. Initialization of var data member works fine below :-

class Person(){
    var name : String = _ 
    def this(n : String) {
      this()
      this.name = n
    }
  }
like image 625
mogli Avatar asked Jul 27 '15 19:07

mogli


People also ask

Should I initialize variable within constructor or outside constructor?

I recommend initializing variables in constructors. That's why they exist: to ensure your objects are constructed (initialized) properly. Either way will work, and it's a matter of style, but I prefer constructors for member initialization.

Can we initialize variable in constructor?

You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.

How do you initialize an object in a constructor?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

Do you have to initialize all variables in constructor?

It really depends on what member variables you have. If you provide a constructor and don't explicitly initialize a variable in the member initialization list, then it will be default initialized. And this is for every variable. Also, it is interesting to know it can depend on compiler options.


1 Answers

You just can't assign to a val after initialization. In Scala the body of the class IS the constructor, you can see examples here.

In general, you just define all variables in the primary constructor itself as "class parameters": class Person(val name: String) if you need to receive the name for initialization or class Person() { val name = 'Joe' } if it is fixed.

This can be quite surprising coming from Java, as you are used to have constructors that produce the values and build the object directly. For such a case, the best solution is to use apply methods on a companion object:

    class Person(val name: String)
    object Person() {
        def apply(db: SomeDBConnectionWrapper, id: Int) = new Person(db.fetchName(id))
    }

That allows you to call Person(db, 3) to get a new person with custom initialization, but the constructor itself still receives all it needs to construct a new instance where all values are only assigned once.

like image 157
Daniel Langdon Avatar answered Sep 28 '22 01:09

Daniel Langdon