Home » Scala this keyword

Scala this keyword

by Online Tutorials Library

Scala this

In scala, this is a keyword and used to refer current object. You can call instance variables, methods, constructors by using this keyword.

Scala this Example

In the following example, this is used to call instance variables and primary constructotr.

Output:

101 Martin  

Scala Constructor Calling by using this keyword

In the following example this is used to call constructor. It illustrates how we can call constructor from other constructor. You must make sure that this must be first statement in the constructor while calling to other constructor otherwise compiler throws an error.

Output:

Rama 100  

You may also like