paul paul - 1 year ago 73
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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download