Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By sunde887

Thread: "this" keyword

  1. #1
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default "this" keyword

    can i know what is the use of "this" keyword in this context? what is the current object of "this" in this case?

    sorry for my stupidity..~ i have tried to search it on the internet but those weren't the answer which i'm seeking for..~
    Attached Thumbnails Attached Thumbnails "this" keyword-untitled.jpg  

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    This is a program you are developing for an Android phone?

    The operator "this" uses the object's own reference as the constructor for the TextView class. This isn't a compilable and runnable program, so I can't really expand on how its used there.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If a method takes an argument of the current class you are working in you can pass a reference as "this", this refers to the object you are currently working in.

    Let's say you have a class, X, which has in it an object of type Y, object Y's constructor takes an object of x, you can pass this into Y like this

    There are flaws in the below snippet, it's really meant as a basic example to show the idea.

    Java Code:
    public class X{
      private Y y;
      public X(){}
      public void createY(){
        y = new Y(this);
      }
    }
    
    public class Y{
      private X x;
      public Y(X x){
        this.x = x;
      }
    }
    This uses this in two ways, one to pass an argument, the other to differentiate variables(this.x = x;).

  4. #4
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    ok ..~ thanks for the help..~
    is this some sort of shortcut? why aren't they jotted down in the java documentation?
    my friend said that it was a context reference. is it true?
    I have encountered this for several times but didn't know the usage of it and always forget to seek for the answer..~
    I'm just begin to learn how to write android..~ so that i can add a few more lines on my future resume..~ haha

  5. #5
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    ok ..~ thanks for the help..~
    is this some sort of shortcut? why aren't they jotted down in the java documentation?
    my friend said that it was a context reference. is it true?
    I have encountered this for several times but didn't know the usage of it and always forget to seek for the answer..~
    I'm just begin to learn how to write android..~ so that i can add a few more lines on my future resume..~ haha

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    It is documented, but probably not so much in Android documentation. From what I've seen in Android documentation it expects you to already understand at least the basics of Java.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    As with all things Java you ought to learn the basics of Java before trying to code in Swing, JDBC, any number of frameworks, or Android.

  9. #9
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    I do have java basic, just I seldom encounter passing "this" as an argument. And I took java during my second semester in my university level.

    The code (android) was just happened to be chosen by me accidently.~ It did not matter what kind of code from which field that I were going to choose, because I just want to know how it works.

    Anyway, thanks for the helps and i really appreciate..~ hahahhahahaha

  10. #10
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    This isn't the program, just the code for me to learn how to code Android..~

  11. #11
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    I do know on how to use "this" with a field and constructor, but it didn't mention it can be used as an argument..~

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

    The tutorial does not show an example, however this part of the text explains what it does. Anything that can accept the reference of the object that this refers to can take this as a valid argument. I think the examples show alternative ways to how you can use it, more common ways too.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Quote Originally Posted by ryozkidz View Post
    I do know on how to use "this" with a field and constructor, but it didn't mention it can be used as an argument..~
    But since you know that this is a reference to the current object then why wouldn't it be usable as an argument?
    Method arguments that are objects are simply references.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    As others have said "this" is good to use as a variable including as an argument provided it is of the right type.

    In Android terms, the TextView constructor wants an instance of Context in its constructor. (so that the text view knows its context...) The code you posted is typical of an Activity or subclass - it says what should happen when the activity is first started or when the user navigates back to it. Now since an Activity IS-A Context "this" (the activity instance) is just the thing to pass to the TextView constructor.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by ryozkidz View Post
    I do have java basic, just I seldom encounter passing "this" as an argument. And I took java during my second semester in my university level.
    You probably slept through the class explaining it.

Similar Threads

  1. How to use "this" Keyword
    By Gousia in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 01:50 PM
  2. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •