u might think this question is too basic...but i really want a good explanation on 'this' object reference in java. although i read about it in so many books but up till now i still couldnt really understand about 'this'. can someone please help me???
01-10-2003, 12:42 PM
No question is stupid, essentially! The "this" keyword basically refers to the current instance of the class. For example, if you have a book class with the id and name variables then this.id would refer to the id variable and this.name would refer to the name variable. So the effect of String theName = this.name; would be the same as doing String theName = name;. This may seem silly but there are greater uses for it, which I cannot really think of at the moment (in the middle of doing Networks revision for my 2nd year Comptuer Science exams).
-- this would obviously refer to the inner class's string variable not the string in the class that it's in. so what if you want to refer to the outer class's string variable?i once encountered this and had to change the inner class's variable name so i won't have to puzzle it out. but i have read a solution to this and i can't remember it now. would someone please refresh my memory? sheesh........ =)