paul paul - 20 days ago 5
Scala Question

Specify attribute value on constructor

I´ve been looking for this, but I could not find anything.

if I have this class

class A(a1:Int, b1:Int,c1:Int){}


I can not create an instance specifying only one of the values of the constructor as in groovy right?.

new A(b1:1)

Answer

You can provide default values for your class parameters:

// add default values to your class parameters    
class A(a1: Int = 0, b1: Int = 0, c1: Int = 0)

// call the constructor by passing parameters of your choice
val a = new A(b1 = 1)
Comments