Have you tried to read this? (<- link). I don't think I could explain it clearer. Give a try and tell us if you still have doubts.
Christopher Laurenzano wrote:Can someone explain to me exactly what the 'this' keyword does? I seem to be at a loss to understand the definitions I've read so far; rather confusing.
You know by the passed arguments to the constructor during the object creation. Each declared constructor has different parameters list (or don't have at all). So, constructors parameters list (with types) has to match with passed arguments.
Christopher Laurenzano wrote:when you have overladed constructors in a class, how does the compiler know which one is called?
Christopher Laurenzano wrote:Sorry, but I still don't get it. To be honest, I don't think it's written that clearly. But in either case I'm still confused.
"this" refers to the active (currently executing code) object, so in the examples there were arguments of x and y for the method, where it says this.x = x means the x of the object gets the value of the argument and the same for this.y = y.
First of all mind this Java is case sensitive so "THIS"(as mentioned in the title of this post) is not a keyword ,"this" is a keyword.
"this" can be used in 2 places :
Its two main uses occurs :
Now the second part of your question,
First mind this:Compiler tries to find all the error that can be generated in the code ,without even executing a Single line
And the argument list you provide while creating instance will help in identification of particular constructor
As this on its own it means the whole of the object you happen to be working in.
As this. with a dot it means the instance member rather than any local variables.
As this(...); with round brackets it is a constructor call from one constructor to another. It must be first in the constructor. The compiler disambiguates overloaded constructors the same way as for methods: look at the type and number of arguments. If you write
this(123, 456, 789);
it will look for a constructor which can take three ints or maybe three longs or int... or a three Integers as parameters. If there is any confusion it looks for which it thinks the more specific. If it can't sort them out it will fail to compile. The details are in the Java® Language Specification (=JLS), but that can be very difficult to read. There is a link in the JLS to its §15.9 which will explain how it is resolved. That is bit will be difficult to understand, I expect.