Member Avatar for joankim

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".

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;
    }
}
Member Avatar for joankim

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?

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
    }
}
Member Avatar for joankim

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);

Member Avatar for joankim

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?

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.

commented: thanks for helping me out guys +2

x = this.x would make global x = the methods x.

commented: thanks for helping me out guys +2
Member Avatar for joankim

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.

commented: I still dont know what you meant... Maybe explain it instead of pointing out my mistakes. +0

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.