1
public class MyClass {

    private char e; 

    public void intializeVariable(char z) {
       this.e = z;
    }
     public void intializeVariableWithoutThis(char z) {
       e = z;
    }

    public static void main(String[] args) {
    MyClass mC =  new MyClass();
        System.out.println(mC.e);
        mC.intializeVariable('m');
        System.out.println(mC.e);
        mC.intializeVariableWithoutThis('n');
        System.out.println(mC.e);
    }

}

The methods initializeVariable and initailizeVariableWithoutThis are doing the same thing I just want to understand what is the difference in setting the value using this.e = z and just doing e = z

Nick Div
  • 5,338
  • 12
  • 65
  • 127

5 Answers5

4

This is used to indicate a field of current object.

public void intializeVariable(char z) {
   this.e = z;
}

If you change the above method to

 public void intializeVariable(char e) {
   this.e = e;
}

This will work normally. Compiler will know the "e" is the parameter and the this.e is the field of the object.

Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
3

For your example, there is no difference, because each of them assigns z to e.

It would only make a difference if the name of the parameter was the same exact name of the instance variable, e.g. both were named e.

public void initializeVariableSameName(char e){
    this.e = e;
}

In that case, e refers to the parameter e and not the instance variable e. It shadows the instance variable e. That means that the only way to access the instance variable is by qualifying it with this, e.g. this.e.

The other case:

    e = e;

would assign the parameter e to itself; nothing useful.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

There is no difference but if you use a parameter with same name as a class variable then you have to use this keyboard

public void intializeVariable(char e) {
   this.e = e;
}
void
  • 7,760
  • 3
  • 25
  • 43
  • you're welcome, yes other answers came when I've been typing this answer and I didn't see them before posting this answer – void Dec 03 '14 at 23:54
  • Thats ok. You never know if some answer might have some additional information. And I have to also wait till 7 minutes mark one as a correct one. – Nick Div Dec 03 '14 at 23:59
1

In this case you don't need to explicitly say that e is one of the field of this. instance because in current scope there are no other variables called e so you can skip this. part (it will be added by compiler automatically). But if your method signature would look like

public void intializeVariable(char e) {
    ..
}

and you would like to assign value passed as e argument to field e you would need to write it as

this.e = e; 
//   ^   ^-- methods local variable
//   |
//   +------ field 
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Quoting oracle docs The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.
Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31