This keyword used to represent the current instance of the class.We use This keyword to differentiate between class instance variable and local variable.
Example:
public class ThisKeywordDemo{
String str = ‘DefaultValue’;
public ThisKeywordDemo(String str){
System.debug(‘–local Value–‘+str);
System.debug(‘–Current Instance Value–‘+this.str)
this.str = str;
}
}
The above example has One Instance Variable & One Local Variable or Method parameter with same name.
str = Refers parameter value.
this.str = Refers the current instance variable value.
To execute this class go to Developer Console, paste the below line then click execute.
ThisKeywordDemo thisExample = new ThisKeywordDemo(‘test This Keyword’);
-
This reply was modified 9 years, 8 months ago by
Ravi.