views:

627

answers:

14

Hello,

I have a question about the use of "this" in Java.

If I write the following class:

public class Example {

      int j;
      int k;

      public Example(int j, int k) {
           j = j;
           k = k;
      }

      public static void main(String[] args) {
           Example exm = new Example(1,2);
           System.out.println(exm.j);
           System.out.println(exm.k);
      }

}

The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:

this.j = j;
this.k = k;

But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?

Are there other cases, when one has to use "this" obligatory? Because, very often one does not write "this".

Thank you.

+6  A: 

If you don't write this, then you assign the argument to itself; the argument variables shadow the instance variables.

Robert Grant
+22  A: 

If you don't write "this.variable" in your constructor, and if you have a local variable (including the function parameter) with the same name as your field variable in the constructor, then the local variable will be considered; the local variable shadows the field (aka class variable).

One place where "this" is the only way to go:

class OuterClass {
  int field;

  class InnerClass {
    int field;

    void modifyOuterClassField()
    {
      this.field = 10; // Modifies the field variable of "InnerClass"
      OuterClass.this.field = 20; // Modifies the field variable of "OuterClass",
                                  // and this weird syntax is the only way.
    }
  }
}
artknish
Nice catch: the horror of inner classes :)
Robert Grant
@Robert: Thanks! Yes, avoid as much as possible :)
artknish
+4  A: 

this is useful when you want to return the object itself

return this;

This is useful because if a class has for example Method1() and Method2(), both returning this, you are allowed to write calls like

object.Method1().Method2()

Also inside a method it can be useful to pass the object itself to another function, during a call.

happy_emi
+3  A: 

In your constructor code, you are assigning variables to themselves. 'j' is the j specified in the argument for the constructor. Even if it was the class variable j defined above, then you are still saying "j = j" ... i.e. j isn't going to evaluate differently on the left and on the right.

  public Example(int j, int k) {
       this.j = j;
       this.k = k;
  }
Phil
+9  A: 

If you say just j in your constructor then the compiler will think you mean the argument in both cases. So

j = j;

simply assigns the value of the argument j to the argument j (which is a pretty pointless, but nonetheless valid statement).

So to disambiguate this you can prefix this. to make clear that you mean the member variable with the same name.

The other use of this is when you need to pass a reference to the current object to some method, such as this:

someObject.addEventListener(this);

In this example you need to refer to the current object as a whole (instead of just a member of the object).

Joachim Sauer
+2  A: 

You assign the parameter to itself in your example.

More generally speaking: If you don't prepend a scope to your variable, the current scope is assumed - which is the function in your case. 'this.j' tells the jre to use the variable j within the object scope - the member variable of the object.

soulmerge
+1  A: 

Just like Robert Grant said, 'this' is how you make it clear that you are referring to a member variable instead of a local variable.

willcodejavaforfood
+2  A: 

What you are experiencing is called variable shadowing. Have a look at this overview for different kinds of variables in Java.

Generally speaking: The Java compiler uses the nearest variable it can find for an assignment. In a method it will first try to find a local variable and then enlarge the focus of its search to class and instance variables.

One habit I personally find good (others don't like it) is prefixing a member variable with m_ and using uppercase for CONSTANT_VARIABLES that don't change their value. Code where variable shadowing is used on purpose is very(!) difficult to debug and work with.

Thorsten79
Constants that never change should be declared final, then it's impossible to overwrite their values accidently.
Mark Renouf
+4  A: 

Another useful approach (though seldom used) is to declare method parameters final:

The following block will not compile, thus alerting you to the error immediately:

  public Example(final int j, final int k) {
       j = j;
       k = k;
  }
Mark Renouf
I don't like using final for local variables unless it is required (but for instance variables I use them always when possible). I find it too much clutter. Instead I use my IDE's code highlighting. See my comment here http://blog.objectmentor.com/articles/2008/10/28/data-rattle#comment-2155
Esko Luontola
A: 

And what about using "this" with methods? Is it optional or there are situations when one needs to use it?

+1  A: 

To answer your follow-up question about this with method, the inner class mentioned by Srikanth is still a valid example of using this: (with method this time)

public class OuterClass
{
    void f() {System.out.println("Outer f()");};
    class InnerClass {
     void f() {
      System.out.println("Inner f()");
      OuterClass.this.f();
     }
    }
}

You have the same situation with anonymous class:

You can refer to the outer class’s methods by:

  • MyOuterClass.this.yOuterInstanceMethod(),
  • MyOuterClass.myOuterInstanceMethod(),
  • or simply myOuterInstanceMethod() if there is no ambiguity.
VonC
A: 

This doesn't quite answer your question, but if you use Eclipse you might find "Assignment has no effect" setting useful. I am sure this will be in other IDEs too.

Hemal Pandya
A: 

You might want to take a look at What is the advantage of having this/self pointer mandatory explicit?
Although using this is not mandatory in Java as you noticed I'm sure it will shed some light on the subject of using this in Java as well.

Piotr Dobrogost
A: 

To avoid this, use an IDE (like Eclipse) and it will generate warnings in this case.

Also, make your fields final unless they absolutely can't be. There are a number of reasons (apart from this one) to do that.

Neil