Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Zyril

Thread: "This" as an argument

  1. #1
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default "This" as an argument

    For whatever reason, the "this" keyword always seems to confuse me in new ways. I understand it's used when namespaces overlap, or when accessing one constructor from another constructor, but I still experience confusion when I see it used as an argument - especially when the code is a snippet and therefor out of context, which I often run into because I use a lot of small tutorials from sites like stackoverflow to learn about random, perhaps obscure features I'm curious about. I'm one of those guys who just has to understand every line of code I read or I get all bent out of shape.

    Why would one need a reference to the object instead a creating a new object of the class instead?

    Is the purpose of the reference to gain access to members and methods of an external class?

    I'm either over-thinking this or missing something basic.
    Last edited by Daimoth; 12-29-2012 at 11:39 PM. Reason: this, argument

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: "This" as an argument

    Hi Daimoth,

    I cannot give you an answer that will put light on all of your questions, though I might be able to add some insight to the "this"-keyword.

    First of all, shadowing:
    This is for instance when you have a class that holds variables a & b, which you want to set to certain values through a method with parameters. It might look something like this:

    Java Code:
    int a;
    int b;
    What would you accomplish with this:

    Java Code:
    public void setVars(int a, int b){
    a = a;
    b = b;
    compared to

    Java Code:
    public void setVars(int a, int b){
    this.a = a;
    this.b = b;
    this refers to the current object for which the method setVars belong. The variables in this object (instance of the class) a and b will be set to the values of the parameters a and b.

    I am using this all the time in this way, because I feel like shadowing is the easiest way to keep track of what your methods and your code does.

    ---

    Why would one need a reference to the object instead a creating a new object of the class instead?
    I'm not sure that I understand this question. Though, I can give you an example of when I use the this keyword in another way.

    Most often I code according to the MVC-pattern, Model View Controller. Everything that is being presented to the user, i.e. a GUI runs from code in the View-class. This contains no logic, only code that will present information or help to present information. It is crucial that the Controller-class has a reference to the View-object, otherwise no information could be presented.

    See it this way (may not be the correct way but how I do it right now):

    Startup-class: this class creates the View object from the View class.
    View-class: Contains GUI code. This class creates the Controller object from the controller class.
    Controller-class: Contains code that handles calls from the View object. Makes calls to the Model object.

    I wont dive deep into the MVC pattern, but look at what the View class does. It creates the Controller object that is being used. This object needs to have a reference to the View object. This can be achieved by passing this as an argument to the constructor of the Controller class. The following code using the this keyword sort this out!

    Java Code:
    public View() {		
    		controller = new Controller(this);
    }
    Hope this help you out a bit more!

    Cheers,
    Z

  3. #3
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: "This" as an argument

    So you're using "this" because your Controller class has a constructor that requires a View object, and you want to give it that specific View object, not a new, separate instance of the View class?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: "This" as an argument

    Think of 'this' as 'I' or 'me', 'my' or 'mine'. The code snippet:

    Java Code:
    void setX(int x) { this.x= x; }
    reads as 'my x gets the value of (the parameter) x' and:

    Java Code:
    anObject.doSomething(this);
    reads as 'anObject should doSomething with me'.

    kind regards,

    Jos
    Daimoth likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: "This" as an argument

    Exactly! As JosAH points out above, always see this as the current object being used. My controller object doesn't want a new View-object but THE View-object that is only once created!
    Daimoth likes this.

  6. #6
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: "This" as an argument

    I understand now, thanks, both of you.

  7. #7
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: "This" as an argument

    this
    represents the current object. The object that already was in the memory. Creating a new object is allocating memory for it in a new address. But using the current one is different from it.

    For ex,
    JDialog jd=new JDialog(this);
    where this could represent the current JFrame class (say for example)

    Whereas,
    JDialog jd=new JDialog(new MyFrame());
    this could represent a new JFrame will be created and it's object is sent to the JDialog, thereby the meaning comes as the dialog is a child for a new JFrame that is created but not for the already existing JFrame.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 8
    Last Post: 11-04-2010, 11:03 PM
  3. what does it mean "pass an argument to a method"
    By edelric666 in forum New To Java
    Replies: 6
    Last Post: 10-31-2010, 11:08 AM
  4. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  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
  •