-1

So I have searched a lot -and by "a lot" I am mean it-, both in this website and in other ones, to realize what the keyword this does in java.

I am following tutorials these days to develop a game for Android. In these tutorials, the uploader puts "this" as a parameter but he doesn't explain why he does it.

What I know so far:

  • It can be used as a parameter (this is the part I get most confused)
  • It can be put like this Fish.this to refer to an outer class (not so sure about this one)
  • It can be used to refer to refer to outer variables (worst definition ever, I know) like this:

public class Humans{

    int name; //aka name1
    public Humans(int name){ //aka name2
        this.name = name; //the name1 = name2
    }

I'd like to have an in depth explanation of that keyword since I find it really confusing and, at the same time, it prevents me from moving on with the tutorials (please don't bother answering if your response is going to be brief, I like to have things clear in my mind because I get confused easily, especially in programming). I am stuck and your help would be much appreciated!

Lazini
  • 43
  • 9
  • 2
    http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html – Steffen Apr 06 '14 at 20:02
  • [java this keyword in google](http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html) – Sotirios Delimanolis Apr 06 '14 at 20:02
  • try searching on stackoverflow. – Raghunandan Apr 06 '14 at 20:02
  • It's not really a duplicate.... – Anubian Noob Apr 06 '14 at 20:04
  • @Raghunandan, in case you didn't notice, I said that I searched a lot both in stackoverflow and in other websites, but I didn't get the answer I was looking for, and I still haven't realised completely what it does. I was just hoping I could get a better answer here, but it seems that the answer is no, I won't get a good answer. – Lazini Apr 08 '14 at 15:26
  • @user3426651 you have many such answers and its a duplicate and closed. If you have a doubt you need to specify what is it and explain clearly what you are looking for which wasn't in this case – Raghunandan Apr 08 '14 at 16:19

1 Answers1

3

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 most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

For example, the Point class was written like this

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}

but it could have been written like this:

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.

You can find good example here(Also they will show the diffrent uses of the "this" keyword) ---> http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

UserFuser
  • 110
  • 12
  • Nice answer! You should add that `this` can be used to refer to outer classes. – Anubian Noob Apr 06 '14 at 20:04
  • Thank you! But I have to add that this is not my words, I found them a while ago when starting coding and it helped me understand the "this" keyword and I thought I would share. @AnubianNoob – UserFuser Apr 06 '14 at 20:06
  • @UserFuser that exact part is clear in my mind. I just can't understand how it can be used as a parameter to a method, like this: `button1.setOnClickListener(this);`. Plus, what I am looking for isn't answered in the [Oracle Docs](http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html), that's why I refered to this website. – Lazini Apr 08 '14 at 15:22
  • OnClickListener is a interface, `button1.setOnClickListener(this);` means that `button1` is implementing OnCLickListener.(Meaning: setting OnClickListener on button1.) `this` refers to "this" object which in this case is button1. In the same way you could write `button2.setOnClickListener(this);` in this case `this` refers to `button2`, and so on. I'm not really good at explaining things, but I hope it helped you to understand. – UserFuser Apr 08 '14 at 21:36
  • Also, lets say you have a class named `SpeedButton`, which extends the class `JButton`(From Swing, can use the interface `ActionListener.`). In the `SpeedButton` class, in a field or the constructor for example, you could write `addActionListener(this);`, and in this case `this` will refer to the same `SpeedButton` object you wrote the `addActionListener(this);`-code in. @user3426651 – UserFuser Apr 08 '14 at 21:42
  • @UserFuser This was actually pretty helpful! Thanks a lot, that really helped! – Lazini Apr 09 '14 at 10:58
  • Glad I could help! Please, if you could mark my answer as accepted, I would greatly appreciate it! @Lazini – UserFuser Apr 09 '14 at 15:28
  • @UserFuser How would I go for that? Yep I am a new one around here. – Lazini Apr 09 '14 at 16:12
  • Well, welcome! This is a really awesome and helpful site! Look at the first answer here: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work . There is a picture showing what to click :) @Lazini – UserFuser Apr 09 '14 at 19:13
  • @UserFuser Oh... I thought I could do that for comments too.... Thanks a lot then! I accepted it :D – Lazini Apr 11 '14 at 11:54