6

As far as I know this is used in following situations:

  1. this keyword is used when we want to refer to instance variable having same name as local variable.
  2. Calling one constructor to other in same class.
  3. Pass the class instance as an argument to the method.
  4. Accessing the outer class variables.

But I have gone through my project code where they are using this in getters like:

class a {
    int time;

    int getValue() {
        return this.time * 5.;
    }
}

As far as I know, every object has its own copy of instance variables and methods, so will returning this way makes any sense. Please clarify.

Stackoverfow problem referred:When should I use "this" in a class?

Community
  • 1
  • 1
mahan07
  • 887
  • 4
  • 14
  • 32
  • 3
    `this` refers to the current object instance, so `this.time` refers to "its own copy of instance variables". It's optional though. Many programmers don't use `this` when referring to instance variables. The code @SrinathGanesh shows is a nice convention for constructors though. – DavidS May 27 '15 at 17:45

4 Answers4

9

Many people (I'm one of them) use this keywords even when it is not explicitely needed.

  • Some people find it more clear to put this in front of anything which belong in the same class, the same logic apply to super.
  • Some people always use this by reflex for a better auto-completion (pop-up appear automatically, lazy programmer) in some IDE.

These points are mostly opinion based and doesn't really matter.

For the other uses, as you mentionned, it can be used to pass parameter to another constructor, pass the class instance or to distinct when you have two variables with the same name.

However, IMO, it is way simplier to just not have multiple variables with the same name.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
6

When you have a method like the following one:

public void setFoo(Foo foo) {
    this.foo = foo;
}

Using this is mandatory. Otherwise, it would assign the argument foo to itself, instead of assigning it to the instance variable.

But that doesn't mean that's the only situation you may use this. The following code is strictly equivalent:

public void setFoo(Foo newFoo) {
    this.foo = newFoo;
}

Although in that case, you might very well write

public void setFoo(Foo newFoo) {
    foo = newFoo;
}

because this is not necessary anymore. That doesn't make it illegal.

So,

int getValue() {
    return time * 5;
}

and

int getValue() {
    return this.time * 5;
} 

are strictly equivalent.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

The keyword 'this' is used to refer to the current Reference object being used. This is just used as syntactic sugar to make it easier to do certain tasks such as invoking constructors in the same class, accessing fields in the same class, parameter types..

Here is an example of two different programming styles. Both do the same thing but the first example uses 'this' to explicitly call the constructor again.

    public class Foo {
    public Foo() { this("Hello"); } //Explicit constructor call
    public Foo(String string) { System.out.print(string); }
    }

    public class Bar {
    public Bar() { new Bar("Hello"); }
    public Bar(String string) { System.out.print(string); }
    }
1

The keyword 'this' is often used from within anonymous classes to reference fields in the containing class. For example:

public class App 
{
    public String myString = "This is Java";
    public App()
    {
        JButton button = new JButton("Test");
        button.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            System.out.println(App.this.myString); // <-- App.this gives access to public fields in App
        }});
    }
}
swingMan
  • 732
  • 1
  • 6
  • 17