this  is a keyword in Java. It can be used inside the Method or constructor of  Class. It(this) works as a reference to the current Object whose Method or constructor is being invoked. The this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.

this keyword with field(Instance Variable)

this keyword can be very useful in the handling of Variable Hiding. We can not create two instance/local variables with the same name. However it is legal to create one instance variable & one local variable or Method parameter with the same name. In this scenario the local variable will hide the instance variable this is called Variable Hiding.

Example of Variable Hiding

  1. class JBT {
  2. int variable = 5;
  3. public static void main(String args[]) {
  4. JBT obj = new JBT();
  5. obj.method(20);
  6. obj.method();
  7. }
  8. void method(int variable) {
  9. variable = 10;
  10. System.out.println("Value of variable :" + variable);
  11. }
  12. void method() {
  13. int variable = 40;
  14. System.out.println("Value of variable :" + variable);
  15. }
  16. }
Output 
  1. Value of variable :10
  2. Value of variable :40
As you can see in the example above the instance variable is hiding and the value of the local variable(or Method Parameter) is displayed not instance variable. To solve this problem use this keyword with a field to point to the instance variable instead of the local variable.

Example of this keyword in Java for Variable Hiding

  1. class JBT {
  2. int variable = 5;
  3. public static void main(String args[]) {
  4. JBT obj = new JBT();
  5. obj.method(20);
  6. obj.method();
  7. }
  8. void method(int variable) {
  9. variable = 10;
  10. System.out.println("Value of Instance variable :" + this.variable);
  11. System.out.println("Value of Local variable :" + variable);
  12. }
  13. void method() {
  14. int variable = 40;
  15. System.out.println("Value of Instance variable :" + this.variable);
  16. System.out.println("Value of Local variable :" + variable);
  17. }
  18. }
Output 
  1. Value of Instance variable :5
  2. Value of Local variable :10
  3. Value of Instance variable :5
  4. Value of Local variable :40

this Keyword with Constructor

“this” keyword can be used inside the constructor to call another overloaded constructor in the same Class. This is called the Explicit Constructor Invocation. This occurs if a Class has two overloaded constructors, one without argument and another with argument. Then the this” keyword can be used to call constructor with argument from the constructor without argument. This is required as the constructor can not be called explicitly.

Example of this with Constructor

  1. class JBT {
  2. JBT() {
  3. this("JBT");
  4. System.out.println("Inside Constructor without parameter");
  5. }
  6. JBT(String str) {
  7. System.out
  8. .println("Inside Constructor with String parameter as " + str);
  9. }
  10. public static void main(String[] args) {
  11. JBT obj = new JBT();
  12. }
  13. }
Output of above program
  1. Inside Constructor with String parameter as JBT
  2. Inside Constructor without parameter
As you can see “this” can be used to invoke an overloaded constructor in the same Class.
Note*:
  • this keyword can only be the first statement in Constructor.
  • A constructor can have either this or super keyword but not both.

this Keyword with Method

this keyword can also be used inside Methods to call another Method from same Class.

Example of this keyword with Method

  1. class JBT {
  2. public static void main(String[] args) {
  3. JBT obj = new JBT();
  4. obj.methodTwo();
  5. }
  6. void methodOne(){
  7. System.out.println("Inside Method ONE");
  8. }
  9. void methodTwo(){
  10. System.out.println("Inside Method TWO");
  11. this.methodOne();// same as calling methodOne()
  12. }
  13. }
Output of above program
  1. Inside Method TWO
  2. Inside Method ONE

Example of this keyword as Method parameter

  1. public class JBTThisAsParameter {
  2. public static void main(String[] args) {
  3. JBT1 obj = new JBT1();
  4. obj.i = 10;
  5. obj.method();
  6. }
  7. }
  8. class JBT1 extends JBTThisAsParameter {
  9. int i;
  10. void method() {
  11. method1(this);
  12. }
  13. void method1(JBT1 t) {
  14. System.out.println(t.i);
  15. }
  16. }