Following is a simple scala class definition, there are 2 constructors defined inside Person class.
Person.scala:
// class that has field and method,
class Person(var name:String, var age:Short) {
def this() {
this("", 0);
}
def this(name:String) {
this(name, 0);
}
def hi() = printf("hi, I am %s!\n", name)
}
// var nobody = new Person();
var eric = new Person("Eric", 12);
eric.hi;
The question is:
- When
agefield is not provided in constructor's argument list, I want to initialize it to null, not 0, what is the proper way to do that. Same requirement for thenamefield.
case class Person(name: Option[String]=None, age:Option[Int] = None)and then you dont need all those boiler plate constructorsnulltoShort(AnyVal) value.