This question already has an answer here:
- When should I use “this” in a class? 17 answers
I'm trying to get an understanding of what the the java keyword this
actually does.
I've been reading Sun's documentation but I'm still fuzzy on what this
actually does.
The keyword
this
can mean different things in different contexts, that's probably the source of your confusion.It can be used as a object reference which refers to the instance the current method was called on:
return this;
It can be used as a object reference which refers to the instance the current constructor is creating, e.g. to access hidden fields:
It can be used to invoke a different constructor of a a class from within a constructor:
It can be used to access enclosing instances from within a nested class:
"this" keyword refers to current object due to which the method is under execution. It is also used to avoid ambiguity between local variable passed as a argument in a method and instance variable whenever instance variable and local variable has a same name.
Example ::
Source: http://javaandme.com/core-java/this-word
The
this
keyword is a reference to the current object.Another way to think about it is that the
this
keyword is like a personal pronoun that you use to reference yourself. Other languages have different words for the same concept. VB usesMe
and the Python convention (as Python does not use a keyword, simply an implicit parameter to each method) is to useself
.If you were to reference objects that are intrinsically yours you would say something like this:
Think of
this
as just a way for a type to say "my". So a psuedocode representation would look like this:The keyword
this
is a reference to the current object. It's best explained with the following piece of code:It's not used a lot unless you have code standard at your place telling you to use the
this
keyword. There is one common use for it, and that's if you follow a code convention where you have parameter names that are the same as your class attributes:One proper use of the this keyword is to chain constructors (making constructing object consistent throughout constructors):
This keyword works the same way in e.g. C#.
The keyword 'this' refers to the current object's context. In many cases (as Andrew points out), you'll use an explicit this to make it clear that you're referring to the current object.
Also, from 'this and super':
*There are other uses for this. Sometimes, when you are writing an instance method, you need to pass the object that contains the method to a subroutine, as an actual parameter. In that case, you can use this as the actual parameter. For example, if you wanted to print out a string representation of the object, you could say "System.out.println(this);". Or you could assign the value of this to another variable in an assignment statement.
In fact, you can do anything with this that you could do with any other variable, except change its value.*
That site also refers to the related concept of 'super', which may prove to be helpful in understanding how these work with inheritance.