Learning Java, I have never used the keyword "this". So I am wondering when it is smart to use it. I know that it does prevent name conflicts:

public class test
{
int x;
int y;

public test(int x, int y)
{
this.x = x;
this.y = y;
}
}

But this code can instead, and, I think, better, be written like this:

public class test
{
int x;
int y;

public test(int a, int b)
{
x = a;
y = b;
}
}

So, could someone please explain to me when it is smart and/or necessary to use "this" in Java?

"this" is a reference to the object that the code is executing in. In a method where the local variables passed as parameters have the same name as class variables, you use this to get access to the class variables. If the names are different,. you do not need to use "this".

Edited 4 Years Ago by NormR1

Your code is right. It actually isn't good practice to name parameters the same names as the variables. "This" is used for self-referencing things that are in the class. Here's an example.

public class classname
{
    private int x;
    private int y;

    classname()
    {
        this(0, 0); //Calls classname's cunstructor with two parameters
    }

    classname(int a, int b)
    {
        x = a;
        y = b;
    }
}

Thank you devninja, I can see why that would be useful. In my view, it looks like "this" is very useful if you are overloading a constructor, and in some cases need to use both of them. Does that sound right?

Would that only work/ be useful for constructors? For methods, you would go methodname() instead right?

Edited 4 Years Ago by joankim

Another use for this is when you need to pass a reference to the current object to a method. For example if the current executing class implements an interface and the method your are calling needs a reference to an instance of that interface.

Yes thats right. To put things in perpective and make things a little easier to understand. Just think that "this" means "this class".

this can also used from a methods perspective another example

public class classname
{
    private int x;
    private int y;

    public void methodname()
    {
        int x;

        this.x = x; //this methods x = global x
    }
}

NormR1, could you please give an example of that? I think I know what you are talking about, but I'm not completely sure.

For a button listener when the current class implements ActionListener:
someButton.addActionListener(this);

this can also used from a methods perspective another example

    public class classname
    {
        private int x;
        private int y;
        public void methodname()
        {
            int x;
            this.x = x; //this methods x = global x
        }
    }

I see. I guess I will need that sooner or later. However, global x = this methods x would be a lot more useful. (x = this.x) Would that work?

NormR1, I'm sorry, I am not familiar with either ActionListener or button listener. Is there an example with for when this is needed in for example Comparable?

Edited 4 Years Ago by joankim

You will be using listeners a lot when you get into GUI coding.

It would work the same for any interface. If the class implements the interface, then you can use "this" with any method that requires an instance of that interface.

Comments
thanks for helping me out guys

That sounds fun :P

So, do you mean something like this:

public class c implements comparable{
        this.compareTo(object);
    }

or

public class c implements comparable{
        object.compareTo(this);
    }

I can see the use for that at least.

Your use of this in the first example has nothing to do with an interface. "this" is not needed there.
I don't know what the second example is trying to show. The compareTo method does not require a Comparable object.

Comments
I still dont know what you meant... Maybe explain it instead of pointing out my mistakes.

Yes you can use the second example that way. If you want to compare the passed object to the object that comparing. But you could do that with out having to send the object you want to campare to the object you want to compare it to.

This question has already been answered. Start a new discussion instead.