-
'this' object reference
hello...
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???
tx!
-
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).
Hope that helps, if not E-mail me at meethoss@btinternet.com.
Meethoss
-
thanx alot for ur explanation Meethoss!u have helped me to understand 'this' with ur simple example..... all da best for ur exams!
-
yeah, "this" is sometimes a headache...
public class About_THIS
{
String string;
public About_THIS( String string )
{
......
}
public About_THIS( String string, String string2 )
{
this( string2 );
string = this.string;
}
the above is clear, right? but what if you have an inner class?
private class Inner_Class //this is inside the class above...
{
String string;
Inner_Class( String string )
{
string = this.string;
}
}
}
-- 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........ =)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks