woodhead92
woodhead92

Reputation: 117

Why is the using of 'this' keyword necessary?

'this' is used to access the current object that is being used. What advantage does it have over passing the object itself since the method is going to access the current object that is being passes anyway.

Upvotes: 0

Views: 2960

Answers (6)

Claus
Claus

Reputation: 150

If I understand you correctly, you want to do

static void f(MyClass self) { 
    // access current class object using self
}

instead of

void f() {
    // access current class object using this
}

and call the method this way

MyClass.f(instance);

istead of this

instance.f();

While this is actually possible you would always have to pass the current object as a parameter if it needs to be accessed. Why not just pass the object implicitly to non-static methods? This is what java does.

When you have a look at constructors, you will notice that there is no way to pass the current class object to it since the object does not exist before the constructor is called. So you could not access the current class object in constructors.

Upvotes: 0

Shishir Kumar
Shishir Kumar

Reputation: 8201

What advantage does it have over passing the object itself since the method is going to access the current object that is being passes anyway.

As per your question on advantage of using this keyword over passing the object itself is this is a final variable in Java whereas the object when passed may or may not be final. Being a final variable, I can clearly see 2 main advantages of using this over passing object itself.

  1. One cannot assign any new value to the current instance of this.

    this = new Foo(); //compilation error; cannot assign value to final variable : this

  2. It can be used in synchronized block.

    synchronized(this){ /*this synchronized block will be locked on current instance*/ }

Apart from this context set up in the question, there are many advantages of using this in Java which you can figure out from other answers.

Shishir

Upvotes: 1

Salah
Salah

Reputation: 8657

There can be a lot of usage of this keyword. In java, this is a reference variable that refers to the current object. Usage of this keyword

Here is given the 6 usage of this keyword.

  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
  3. this keyword can be used to invoke current class method (implicitly)
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this keyword can also be used to return the current class instance.

Read more about this Keywork

Upvotes: 3

Himanshu Joshi
Himanshu Joshi

Reputation: 3399

this is used to access the current class variable or methods. Used to differentiate between instance variables (ivars) and local variables.

Upvotes: 1

fge
fge

Reputation: 121712

It can help in many cases. The most obvious one is in constructors, when the parameter name is the same as an instance variable:

public final class Foo
{
    private final int bar;

    public Foo(final int bar)
    {
        this.bar = bar; // MUST specify "this" here
    }
}

Upvotes: 3

Smutje
Smutje

Reputation: 18123

this is seldom really necessary - if I recall right, only if you have a field with the same name as a local variable and you want to explicitly specify that you want to access the field and not the local variable.

Upvotes: 1

Related Questions