Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, just like in Java, the this
keyword refers to the current instance of the class. It allows you to refer to members of the current object from within an instance method or a constructor.
Here's a look at the various usages of this
in Scala:
Referring to the Current Instance: Often used to differentiate between method or constructor parameters and class members:
class Person(var name: String) { def setName(name: String): Unit = { this.name = name } }
In the example above, this.name
refers to the name
member of the class, while name
refers to the method parameter.
Calling Another Constructor:
Scala allows defining multiple auxiliary constructors using the def this(...)
syntax. If you want to call another constructor from within a constructor, you can use the this
keyword:
class Rectangle { var width: Double = 0.0 var height: Double = 0.0 def this(width: Double) { this() // Calls the primary constructor this.width = width this.height = width } def this(width: Double, height: Double) { this() // Calls the primary constructor this.width = width this.height = height } }
Referring to Outer Instance in Nested Classes:
If you have a nested class and want to refer to the outer class instance, you can use this
with a qualifier:
class Outer { outer => val outerField = "Outer Field" class Inner { def printOuterField(): Unit = { println(outer.outerField) } } }
Here, outer
is a self-type identifier. Inside the Inner
class, you can use outer
to refer to the enclosing Outer
class instance.
Self-type Annotations:
this
can also be used in a self-type annotation to declare that a trait must be mixed into another trait/class which satisfies the type of the self-type:
trait User { def username: String } trait Tweeter { this: User => // Enforces that Tweeter can only be mixed into User def tweet(tweetText: String) = println(s"$username: $tweetText") }
Here, the Tweeter
trait specifies that it can only be mixed into classes/traits that also mix in or extend User
or have a username
member.
In summary, the this
keyword in Scala is a reference to the current instance of a class and can be used in a variety of contexts, much like in other object-oriented languages. However, Scala introduces a few additional usages, especially in the context of nested classes and traits.
Scala this
Keyword Explanation:
In Scala, the this
keyword refers to the current instance of the class or trait. It is used to differentiate between class members and local variables when they have the same name.
class MyClass { val x = 42 def printX(): Unit = { println(this.x) } }
How to Use 'this' in Scala:
Use this
to access instance variables or methods within the class.
class MyClass { val x = 10 def printValue(): Unit = { println(this.x) } } val obj = new MyClass obj.printValue()
When to Use 'this' in Scala:
Use this
when there is a need to disambiguate between instance variables and local variables with the same name.
class MyClass(val x: Int) { def isEqual(x: Int): Boolean = { this.x == x } }
Implicit vs Explicit 'this' in Scala:
In many cases, Scala can infer the use of this
implicitly. Explicit use is required only when disambiguation is necessary.
class MyClass(val x: Int) { def isEqual(x: Int): Boolean = { this.x == x // 'this' is optional here } }
Using 'this' in Constructors in Scala:
this
can be used in constructors to refer to another constructor of the same class.
class MyClass(val x: Int, val y: Int) { def this(x: Int) { this(x, 0) } }
Avoiding Ambiguity with 'this' in Scala:
Use this
to avoid ambiguity when there is a local variable with the same name as an instance variable.
class MyClass(val x: Int) { def printX(x: Int): Unit = { println(this.x) // Refers to the instance variable println(x) // Refers to the local variable } }