this keyword in Java

By: Jagan  

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class' type is permitted.

To better understand what this refers to, consider the following version of Box( ):

// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}

The use of this is redundant, but perfectly correct. Inside Box( ), this will always refer to the invoking
object. While it is redundant in this case, this is useful in other contexts, one of which is explained in the next section.

Instance Variable Hiding

As you know, it is illegal in Java to declare two local variables with the same name inside the same or enclosing scopes. Interestingly, you can have local variables, including formal parameters to methods, which overlap with the names of the class' instance variables. However, when a local variable has the same name as an instance variable, the local variable hides the instance variable. 

This is why width, height, and depth were not used as the names of the parameters to the Box( ) constructor inside the Box class. If they had been, then width would have referred to the formal parameter, hiding the instance variable width. While it is usually easier to simply use different names, there is another way around this situation. Because this lets you refer directly to the object, you can use it to resolve any name space collisions that might occur between instance variables and local variables. For example, here is another version of Box( ), which uses width, height, and depth for parameter names and then uses this to access the instance variables by the same name:

// Use this to resolve name-space collisions.
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}

A word of caution: The use of this in such a context can sometimes be confusing, and some programmers are careful not to use local variables and formal parameter names that hide instance variables. Of course, other programmers believe the contrary—that it is a good convention to use the same names for clarity, and use this to overcome the instance variable hiding. It is a matter of taste which approach you adopt.

Although this is of no significant value in the examples just shown, it is very useful in certain situations.

This is an extract from the book: Java 2 - The Complete Reference by Herbert Schildt.




Archived Comments

1. I really like it when folks get together and share opinions.
Great site, continue the good wo

View Tutorial          By: mobile phone unlock at 2017-07-07 00:23:59

2. it is nice idea.easily understand
View Tutorial          By: pavithra at 2016-01-28 10:21:58

3. it is very good for best practice of basics........java
View Tutorial          By: sushil ghimire at 2015-03-01 13:27:57

4. This explaination is good to understand the basic use of this keyword.But can add more examples to s
View Tutorial          By: Nisha Thankachan.F at 2013-02-13 05:29:13

5. Clear explanation...
View Tutorial          By: Nisha at 2013-01-31 05:10:29

6. super explination.
View Tutorial          By: nagarjun reddy at 2013-01-19 13:55:16

7. You explained the this Keyword very well.
"this keyword create differences between funct

View Tutorial          By: Karan at 2012-12-04 11:27:35

8. I want to know that what is the main use of the "this" key-word?
Is the this key wo

View Tutorial          By: Krunal Patel at 2012-09-17 08:11:19

9. No it won't work...
View Tutorial          By: Arin at 2012-08-17 09:59:40

10. for ex

public class test()
{
int a;
test()
{
this(

View Tutorial          By: Anundhara at 2012-08-11 06:54:57

11. this total example is copy paste from the Herbert Schildt's Java book......!!!! :P
View Tutorial          By: Parna at 2012-06-03 05:16:37

12. Learn about difference between this and super
<a href="http://www.extreme-java.com/w

View Tutorial          By: Java this keyword at 2012-05-26 13:32:09

13. this operator's simple program http://exambuk.com
View Tutorial          By: shikhil at 2012-04-01 11:01:41

14. ""this"" is a implicit keyword or object, created by JVM and supplied to every j
View Tutorial          By: Gangireddy at 2012-02-15 08:35:18

15. we generally use this keyword for referring to the variables..we actually refer the non static metho
View Tutorial          By: madhu at 2012-02-07 14:11:57

16. it s good ,understanding clearly and with out confusion
View Tutorial          By: vasudeva kumar at 2012-01-06 14:04:30

17. THANX :)
View Tutorial          By: Saurabh at 2012-01-02 16:13:06

18. Copy Pasted from The Complete Reference Book
View Tutorial          By: Kumar Abhinav at 2011-12-20 11:13:32

19. @pankaj because u need to cast it. when u add nums it get converted to integer which is bigger in s
View Tutorial          By: raziq at 2011-12-18 16:06:09

20. i want to knew about byte datatype. why did not byte content byte value like example

View Tutorial          By: pankaj kr tiwari at 2011-12-02 04:37:43

21. One should think of "this" the same as "Mr" and "Mrs" against people's
View Tutorial          By: Mrammaji at 2011-09-26 08:31:24

22. Very good description. I was quite confused before reading this. Clears it up a bit.
View Tutorial          By: Donna at 2011-07-31 03:46:05

23. This is plagiarism. Taken from the eBook, "Java The Complete Reference - 7th Edition"... I
View Tutorial          By: anonymous at 2011-07-28 19:57:01

24. sir every time we are using this.wedth(for example purpose)why we need this.wedth insted we can use
View Tutorial          By: praveen kumar at 2011-06-28 01:33:44

25. very good description ,clear and simple
View Tutorial          By: swapnil at 2011-06-24 06:33:10

26. Thank u very much, your topic is very clear, to the point & very helpful.
View Tutorial          By: Ahmed Samir at 2011-05-26 23:35:22

27. the article is very useful...!
View Tutorial          By: pravin salame at 2011-05-20 11:58:28

28. It's good but not easily understandable....
View Tutorial          By: koshal at 2010-12-19 23:32:05

29. I'm sorry but your closing statement...

"this is of no significant value in the

View Tutorial          By: Al Scherer at 2009-09-15 09:33:24

30. Yes the article is good and useful...!
View Tutorial          By: Regalla Naresh Reddy at 2009-04-12 23:16:24


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial