The default constructor is the one you define when you declare your class
Example:
class greeting(name:String) { ... }
You can also define a default constructor to not accept any parameters, as in your code
class greeting { ... }
Then you can add additional constructors. All constructors added to the class must call another constructor as the first constructor statement. If you omit this, you will get "this expected but found identifier."
Let's look at an example:
class classconstructor { var iter:Int = 0 def this(it:Int) = { this() iter = it; moveNext(); } def moveNext() = { println(iter) } } object App { def main(args:Array[String]) { val x = new classconstructor() val y = new classconstructor(200) } }
In the above code, new classconstructor () does nothing, because an empty constructor has no body. and also new classconstructor (200) executes an empty constructor + new, where you can add additional code, for example, calling the moveNext () method. In this case, it prints 200 on the console.
Carlos Quintanilla
source share