this keyword in Java

What is this

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

class JBT {

	int variable = 5;

	public static void main(String args[]) {
		JBT obj = new JBT();

		obj.method(20);
		obj.method();
	}

	void method(int variable) {
		variable = 10;
		System.out.println("Value of variable :" + variable);
	}

	void method() {
		int variable = 40;
		System.out.println("Value of variable :" + variable);
	}
}

Output of the above programme

Value of variable :10
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

class JBT {

	int variable = 5;

	public static void main(String args[]) {
		JBT obj = new JBT();

		obj.method(20);
		obj.method();
	}

	void method(int variable) {
		variable = 10;
		System.out.println("Value of Instance variable :" + this.variable);
		System.out.println("Value of Local variable :" + variable);
	}

	void method() {
		int variable = 40;
		System.out.println("Value of Instance variable :" + this.variable);
		System.out.println("Value of Local variable :" + variable);
	}
}

Output of the above program

Value of Instance variable :5
Value of Local variable :10
Value of Instance variable :5
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

class JBT {

	JBT() {
		this("JBT");
		System.out.println("Inside Constructor without parameter");
	}

	JBT(String str) {
		System.out
				.println("Inside Constructor with String parameter as " + str);
	}

	public static void main(String[] args) {
		JBT obj = new JBT();
	}
}

Output of above program

Inside Constructor with String parameter as JBT
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

class JBT {

	public static void main(String[] args) {
		JBT obj = new JBT();
		obj.methodTwo();
	}
	void methodOne(){
		System.out.println("Inside Method ONE");
		}

	void methodTwo(){
		System.out.println("Inside Method TWO");
		this.methodOne();// same as calling methodOne()
	}
}

Output of above program

Inside Method TWO
Inside Method ONE

 

Example of this keyword as Method parameter

public class JBTThisAsParameter {

	public static void main(String[] args) {
		JBT1 obj = new JBT1();
		obj.i = 10;
		obj.method();
	}

}

class JBT1 extends JBTThisAsParameter {
	int i;

	void method() {
		method1(this);
	}

	void method1(JBT1 t) {
		System.out.println(t.i);
	}
}
Series Navigation<< Different ways to create an object in JavaJava Static Keyword >>
By | 2017-08-14T10:49:24+00:00 November 2nd, 2012|Core Java|44 Comments

About the Author:

44 Comments

  1. Janith September 11, 2013 at 12:51 am - Reply

    Example of this with Constructor has mistake.Can you please make it correct.

    • admin September 11, 2013 at 3:47 am - Reply

      Hi Jenith,

      I have corrected the output of given program(this keyword for constructor).
      Thanks for pointing out this problem. If you find more problems do let me know.

      Thanks

      • aa January 19, 2016 at 9:45 am - Reply

        i dont know

  2. arun January 19, 2014 at 8:12 pm - Reply

    this was very helpful..thanks

    • sunny leone February 16, 2017 at 5:00 pm - Reply

      I Know that no need to tell

  3. mukesh February 5, 2014 at 5:38 am - Reply

    very nice tutorial .

    • n January 20, 2016 at 10:47 am - Reply

      okk

  4. krishna February 16, 2014 at 12:35 pm - Reply

    Very nice explanations…

    Thanks alot …

  5. yasir February 20, 2014 at 5:48 am - Reply

    nice

  6. root March 10, 2014 at 5:06 pm - Reply

    Thank you… it helped a lot ..!

  7. Baskar June 21, 2014 at 12:12 pm - Reply

    good help dear

  8. DannyS June 26, 2014 at 1:33 am - Reply

    Scoured the internet to get clarification on this (pun intended). The light bulb finally went on. Thanks.

  9. Abhishek July 22, 2014 at 6:11 am - Reply

    the given example this keyword is not best use in method level because without this you can call method…………………………………………………. so please give me best example of this keyword in method level………

    • Vivekanand Gautam July 26, 2014 at 6:32 am - Reply

      Hi Abhishek,

      I am agree with you given example is not best example for this @Method level. But i couldn’t think of any other example for the same. Meanwhile i have added an example for Method parameter. WHich might be helpful to you. Would update the article in future in case i get any example.

      Thanks

  10. Salil Chawla August 22, 2014 at 8:58 am - Reply

    Very helpful tutorial

  11. Mubeen ali September 9, 2014 at 4:40 am - Reply

    First of all i thank you
    this tutorial was very useful for me.

  12. Nishant September 24, 2014 at 4:18 pm - Reply

    really a very cool website.liked your tutorials,very simple and easy

  13. ram October 15, 2014 at 10:40 am - Reply

    i need a answer for following program
    write a java program to implement the usage of “this” keyword for the following cases
    1. to refer the current class instance variable.
    2. to invoke the current class constructor
    3. to invoke the current class method
    4. to return current class instance
    i need all in one programs with simple codeing…….
    i am waiting …!

    • Prabhakar November 14, 2015 at 1:54 am - Reply

      import java.util.Scanner;

      class Bank
      {
      int accno;
      String name;
      int bal;

      Bank(int accno,String name)
      {
      this.accno=accno;
      this.name=name;

      }
      Bank(int accno,String name,int bal)
      {
      this(accno,name);
      this.bal=bal;
      }

      public void show_details()
      {
      System.out.println(“Accno=”+accno);
      System.out.println(“Name=”+name);
      System.out.println(“Balance=”+bal);
      }
      public void deposit()
      {
      System.out.println(“Enter Amount to be deposit”);
      Scanner sc=new Scanner(System.in);
      int amount=sc.nextInt();
      bal=bal+amount;
      show_details();
      }
      }
      public class Ex_this
      {

      public static void main(String[] args) {
      Bank b1=new Bank(1001,”Hari”,1000);
      b1.show_details();
      b1.deposit();

      }

      }

  14. yogaraj October 29, 2014 at 3:32 pm - Reply

    Very nice.am easily understand

  15. Sandu November 19, 2014 at 4:31 am - Reply

    Nice explanation. Thank you so much.

  16. mukta January 19, 2015 at 11:43 am - Reply

    I is not declared in the example of method to parameter, not necessary??

  17. mukta January 19, 2015 at 11:43 am - Reply

    I mean method parameter.. Isn’t generate any error??

  18. dinesh sharma February 3, 2015 at 10:35 am - Reply

    very nice explanation,thanx!

  19. Ashish June 20, 2015 at 8:21 am - Reply

    Nice one!!

  20. Aqib Karim October 30, 2015 at 7:27 pm - Reply

    Very Nice

  21. RammohanReddy February 12, 2016 at 6:54 am - Reply

    good

    • nura April 26, 2016 at 2:17 pm - Reply

      thanks

  22. nura April 26, 2016 at 2:19 pm - Reply

    it helped me a lot

  23. pooja May 30, 2016 at 11:51 am - Reply

    Thank u soo much for helped me.. a good example

  24. Hash June 4, 2016 at 10:05 pm - Reply

    Thank you so much

  25. Hash June 4, 2016 at 10:07 pm - Reply

    Can you please give the output for the last Example of this keyword as Method parameter. I need only this output for understanding

    • Hemanth Bollamreddi July 5, 2016 at 2:03 pm - Reply

      IT PRINTS THE VALUE OF i ( possibly zero)

  26. Srinivas July 28, 2016 at 4:22 am - Reply

    this one easy understanding program thanks…

  27. Hena Girl August 12, 2016 at 9:52 am - Reply

    If some one needs to be updated with most recent technologies afterward he must be visit this website and be up to date daily.

  28. Ramakrishna September 1, 2016 at 5:18 am - Reply

    very nice

  29. anon September 5, 2016 at 9:06 am - Reply

    where is the output of the last program?

  30. Swad September 15, 2016 at 10:06 am - Reply

    got a clean idea.. Thanks.

  31. Anil October 15, 2016 at 2:14 pm - Reply

    Nice…very helpful

  32. Bobby Smither November 10, 2016 at 3:27 pm - Reply

    “This” helped a lot

  33. arvind yadav November 14, 2016 at 5:27 pm - Reply

    very nice explanation,thnks alot——

  34. shruthi November 15, 2016 at 11:15 am - Reply

    Good examples.

  35. Rj Awais November 20, 2016 at 12:02 pm - Reply

    wow
    nice sharing sir love this site……..

  36. Goran August 14, 2017 at 12:03 pm - Reply

    Great and complete explanation of the word “this”. Something basic that many faculty professors wont tell their students…

Leave A Comment