What's the difference between calling a method with/without this keyword

what'd
what w
whatsapp app
what's
what c
what does
how to pronounce what
what h

I have a question. Please don't mark it as duplicate, Go through the question once. I can't find an answer to this specific situation/condition, If you feel it has a specific answer then only mark duplicate. Marking it duplicate makes my question remain a question without an answer.

What's the difference between calling a method with/without this as a keyword. Which one is better? The question specifically applies for a single class.

Please have a look at the sample code below to fully understand the question.

public class ThisSample {
public static void main(String[] args) {
    ThisSample sample = new ThisSample();
    sample.methodOne();
}
public void methodOne() {
    System.out.println("Method 1 called");
    this.methodTwo();          //Line 1
    methodTwo();               // Line 2
}
public void methodTwo() {
    System.out.println("Method 2 called");
}
}

What difference (Advantage/disadvantage/implication) does the 2 lines (Line 1 & Line 2) in the code make?

Thanks & Regards, Yadvendra


'This' task is to differentiate object property from method parameter. In presented code usage of this does nothing. However the most common use is like in this example:

public class Service {

    private ServiceA serviceA;
    private ServiceB serviceB;

    // Here 'this' is used to make sure that class instance 
    // properties are filled with constructor parameters

    public Service(ServiceA serviceA, ServiceB serviceB) {
       this.serviceA = serviceA;
       this.serviceB = serviceB;
    }

}

Quickly send and receive WhatsApp messages right from your computer. Quickly send and receive WhatsApp messages right from your computer.


this is used to specify that you're talking about the method methodTwo from the current instance of the class ThisSample.

If you'd have another class called AnotherSample:

public class AnotherSample{
    public static void methodThree()
    {
        // some code
    }
}

You could use the method methodThree by calling it as follows AnotherSample.methodThree();.

In summary: this just specifies that you're using the instance of the class you're currently coding in.

what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. informal a punishment or reprimand (esp in the phrase give (a person) what for) 7. what have you someone, something, or somewhere unknown or unspecified: cars, motorcycles, or what have you. 8.


In the example, you have given, the difference is nothing. Let me modify your code a bit:

public class ThisSample {
    int variable;
    public static void main(String[] args) {
        ThisSample sample = new ThisSample();
        sample.methodOne(3);
        sample.methodTwo(5);
    }
    public void methodOne(int variable) {
        this.variable = variable;
        System.out.println("variable is: " + this.variable);
    }
    public void methodTwo(int variable) {
        variable = variable;
        System.out.println("variable is: " + this.variable);
    }
}

Here, for method 2, you must use this.variable to set the value in the instance variable. Otherwise, both method will print 3 here. The second method is also printing three because you set 3 in method one.

Now in method 2, in

variable = variable

line, both variable are paramater of mathod 2. But when you are writing,

this.variable = variable;

you are telling, left one is instance variable of this object and right part is assigned to instance variable of this object.

Edit:

If you want to know "which is more preferred", then see this link too. Here using this is said "redundant". Link is: https://softwareengineering.stackexchange.com/a/113434/162116

Here, it is also said that, I should refactor the code if I actually need this to deduce the instance variable.

Usage Note: When what is the subject of a clause, it takes a singular verb if the word or phrase that completes the sentence (the complement) is singular, as in I​  noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.


Dr. Megan Ranney spent last week testifying about coronavirus in front of Congress. Here's what she says we know, and what we still don't  WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.


Here's the latest on what's happening with coronavirus in the North State, including Redding, the rest of Shasta County and Siskiyou County. In current usage that refers to persons or things, which chiefly to things and rarely to subhuman entities, who chiefly to persons and sometimes to animals. The notion that that should not be used to refer to persons is without foundation; such use is entirely standard.


For the most part, since the last recession, this has been true. But what started as a health crisis has evolved into a redefinition of work, with an  This is the ORIGINAL "What What (In the Butt)" video. The South Park version aired over a year after we made this video. There's been a lot of confusion about this. We can assure you, we are not