this keyword in Java

By: Jagan Viewed: 194 times  Printer Friendly Format    


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.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

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

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

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

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

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

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

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

7. sir every time we are using this.wedth(for example
View Tutorial          By: praveen kumar at 2011-06-28 01:33:44

8. This is plagiarism. Taken from the eBook, "Ja
View Tutorial          By: anonymous at 2011-07-28 19:57:01

9. Very good description. I was quite confused before
View Tutorial          By: Donna at 2011-07-31 03:46:05

10. One should think of "this" the same as &
View Tutorial          By: Mrammaji at 2011-09-26 08:31:24

11. i want to knew about byte datatype. why did not by
View Tutorial          By: pankaj kr tiwari at 2011-12-02 04:37:43

12. @pankaj because u need to cast it. when u add num
View Tutorial          By: raziq at 2011-12-18 16:06:09

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

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

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

16. we generally use this keyword for referring to the
View Tutorial          By: madhu at 2012-02-07 14:11:57

17. ""this"" is a implicit keyword
View Tutorial          By: Gangireddy at 2012-02-15 08:35:18

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

19. Learn about difference between this and super

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

20. this total example is copy paste from the Herbert
View Tutorial          By: Parna at 2012-06-03 05:16:37

21. for ex

public class test()
{

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

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

23. I want to know that what is the main use of the &q
View Tutorial          By: Krunal Patel at 2012-09-17 08:11:19

24. You explained the this Keyword very well.
&

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

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

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

27. This explaination is good to understand the basic
View Tutorial          By: Nisha Thankachan.F at 2013-02-13 05:29:13

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

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

30. I really like it when folks get together and share
View Tutorial          By: mobile phone unlock at 2017-07-07 00:23:59