'this' object reference


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: 'this' object reference

  1. #1
    Join Date
    Dec 2002
    Posts
    10

    Question '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!

  2. #2
    Join Date
    Oct 2002
    Posts
    66
    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

  3. #3
    Join Date
    Dec 2002
    Posts
    10
    thanx alot for ur explanation Meethoss!u have helped me to understand 'this' with ur simple example..... all da best for ur exams!

  4. #4
    Join Date
    Nov 2002
    Posts
    138
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center