-3


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

Droy
  • 173
  • 2
  • 17
  • in this example: nothing. this just points to the current instance of your class for which the method runs – Stultuske Oct 18 '18 at 07:32
  • @Stultuske The example is just for clarifying the question. My question is regarding the concept and internal working. – Droy Oct 18 '18 at 07:34
  • First of all, none of those methods are static. The print is incorrect. – Dezso Gabos Oct 18 '18 at 07:37
  • @Yadvendra always make sure the code you show is representative for the code you're asking about. if your code is just for clarifying the question but conclusions based on it don't apply to your code: it really doesn't clarify anything. – Stultuske Oct 18 '18 at 07:42
  • @Stultuske I feel the question is properly explanatory, & so is the code. Still I'll take care. – Droy Oct 18 '18 at 07:47
  • @DezsoGabos Updated. Shall I expect a answer now? :) – Droy Oct 18 '18 at 07:48
  • 1
    @Yadvendra again: in the example you posted, there is no functional difference or advantage. – Stultuske Oct 18 '18 at 07:49
  • @Stultuske Without the example, can you please let me know the difference or preferred practice in this/any situation? – Droy Oct 18 '18 at 07:57
  • @Yadvendra for a simple example, check Mukit09's answer. but you should definitely visit the links on top of the page. – Stultuske Oct 18 '18 at 07:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182072/discussion-between-yadvendra-and-stultuske). – Droy Oct 18 '18 at 08:01
  • In your case there is no difference between both ways as they will generate same bytecode (will be executed in same way). For coding style difference see questions like: [Excessive use “this” keyword in Java](https://softwareengineering.stackexchange.com/q/211319), [What is the accepted style for using the `this` keyword in Java?](https://softwareengineering.stackexchange.com/q/113430) – Pshemo Oct 18 '18 at 08:23

3 Answers3

0

'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;
    }

}
Chlebik
  • 646
  • 1
  • 9
  • 27
0

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.

  • Nope, you can't call the method like `AnotherSample.methodThree();` because it isn't static. – Thomas Oct 18 '18 at 07:41
  • 1
    The statement you made is false. `This` references to the current instance, not referencing the class in general. – dbl Oct 18 '18 at 07:42
  • @Thomas You're right I made a careless mistake. –  Oct 18 '18 at 07:44
  • @dbl Meant that I just used the wrong wordcombination. I fixed my answer. –  Oct 18 '18 at 07:46
  • @Stultuske I fixed it in the top and fixed it in the bottom as well a minute ago. I don't quite know what you're talking about. –  Oct 18 '18 at 07:52
  • the edit of the bottom wasn't visible here. either way, it has nothing to do what you are coding in with, since coding in a class and running code is not the same. this points to the current instance of the class it is in. [EDIT] the history shows the question was edited after my remark about the last line. probably why it didn't show (yet) – Stultuske Oct 18 '18 at 07:58
  • 2
    You can use `this` to call a `static` method. Try it!! In fact, there is no real *practical* use for using `this.methodName` ... and that is what the Question is about. – Stephen C Oct 18 '18 at 07:58
  • @StephenC it is included in some companies code styling conventions :) – dbl Oct 18 '18 at 08:05
  • 2
    Which doesn't contradict what I just said :-) – Stephen C Oct 18 '18 at 08:08
0

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.

Mukit09
  • 2,956
  • 3
  • 24
  • 44
  • Thanks for the reply. I know the concept for the variables. I was just curious to know which method should be preferred to call other method. With this or without. – Droy Oct 18 '18 at 08:00
  • Oh, I also edited my answer as your expectation. Thanks! @Yadvendra – Mukit09 Oct 18 '18 at 08:51