1

I'd like to better understand what is the difference in referring to a class field by using this.field and field alone as in

this.integerField = 5;

and

integerField = 5;
user207421
  • 305,947
  • 44
  • 307
  • 483
ripkars
  • 85
  • 3
  • 10

6 Answers6

7

this keyword refers to the current object. usually we use this.memberVariable to diffrentiate between the member and local variables

private int x=10;

     public void m1(int x) {
      sysout(this.x)//would print 10 member variable
      sysout(x); //would print 5; local variable
      } 

   public static void main(String..args) {
      new classInst().m1(5);

   }

Off from the concrete question, the use of this In Overloaded constructors:

we can use this to call overloaded constructor like below:

public class ABC {
     public ABC() {
      this("example");to call overloadedconstructor
      sysout("no args cons");
     }
      public ABC(String x){
         sysout("one argscons")
        }

 }
PermGenError
  • 45,977
  • 8
  • 87
  • 106
4

The use of this keywords lets you disambiguate between member variables and locals, such as function parameters:

public MyClass(int integerField) {
    this.integerField = integerField;
}

The code snippet above assigns the value of local variable integerField to the member variable of the class with the same name.

Some shops adopt coding standards requiring all member accesses to be qualified with this. This is valid, but unnecessary; in cases where no collision exists, removing this does not change the semantic of your program.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

When you are in an instance method, you may need to specify to which scope you refer a variable from. For example :

private int x;

public void method(int x) {
    System.out.println("Method x   : " + x);
    System.out.println("Instance x : " + this.x);
}

While, in this example, you have two x variables, one is a local method variable and one is a class variable. You may distinguish between the two with this to specify it.

Some people always use this before using a class variable. While it is not necessary, it may improve code readability.

As for polymorphism, you may refer to the parent class as super. For example :

class A {
    public int getValue() { return 1; }
}
class B extends A {
    // override A.getValue()
    public int getValue() { return 2; }

    // return 1 from A.getValue()
    // have we not used super, the method would have returned the same as this.getValue()
    public int getParentValue() { return super.getValue(); }   
}

Both keywords this and super depend on the scope from where you are using it; it depends on the instance (object) you are working with at run-time.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
1

It's exactly the same. Because you often type this.xyz it's a shortcut that means the same thing if there is a field by that name and there isn't a local variable that shadows it.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • 1
    Not necessarily, depends on scope. If you also have a local variable or a method argument called `integerField`, then to access the instance variable `integerField`, the `this` must be specified. – Muel Oct 30 '12 at 00:05
  • I think I made the edit to my answer at the same time you made that comment! – Joe Oct 30 '12 at 00:07
  • that always happens with me and SO! – Muel Oct 30 '12 at 00:08
0

Though they look and act the same, there is a difference when the same name is shared between a field and a method argument, e.g.:

private String name;

public void setName(String name){

    this.name = name;

}

name is the passed parameter, and this.name is the proper class field. Notice that typing this.... prompts you a list of all the class fields [and methods] in many IDEs.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
0

From the Java tutorials:

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.

So, when you call a method within a object the call looks like this:

public class MyClass{

    private int field;

    public MyClass(){
        this(10); // Will call the constructor with a int argument
    }

    public MyClass(int value){
    }

    //And within a object, the methods look like this
    public void myMethod(MyClass this){ //A reference of a object of MyClass
        this.field = 10; // The current object field
    }

}
ElderMael
  • 7,000
  • 5
  • 34
  • 53