-1

I am bit confused when to use this keyword. I know usually we use them to set parameters in constructors or if we have multiple constructors, however, which approach below is correct?

public class ResultsPage {

    private WebDriver driver;
    private JavascriptExecutor jsx;    
    private Selector selector;
    private WaitFor waitFor;
    private WebElementUtilities webElementUtilities;

    public ResultsPage(WebDriver driver) {
        this.driver = driver;
        this.webElementUtilities = new WebElementUtilities(driver);
        this.selector = new ReactSearchResultsPageSelector();
        this.waitFor = new WaitFor();            
        this.jsx = (JavascriptExecutor) driver;
    }

}

or

public class ResultsPage {

    private WebDriver driver;
    private JavascriptExecutor jsx;    
    private Selector selector;
    private WaitFor waitFor;
    private WebElementUtilities webElementUtilities;

    public ResultsPage(WebDriver driver) {
        // I used this here since the parameter has the same name.
        this.driver = driver;

        // I did not use this here but should I?
        webElementUtilities = new WebElementUtilities(driver);
        selector = new ReactSearchResultsPageSelector();
        waitFor = new WaitFor();            
        jsx = (JavascriptExecutor) driver;
    }

}
Robben
  • 457
  • 2
  • 8
  • 20
  • See [this Q&A](http://stackoverflow.com/q/15715241/335858) – Sergey Kalinichenko Dec 23 '15 at 20:37
  • @GuntherFox I personally wouldn't link a C# answer for a Java question, even if it does technically answer the same question. – SGM1 Dec 23 '15 at 20:55
  • Using `this` is a way of being explicit in that you are referencing the current instance of the class you're in. In you're case you can be even more explicit using `ResultPage.this` or even `your.pacakage.ResultPage.this`. This may be useful when dealing with anonymous or even inner classes. Not best practice being too explicit or referencing outer class inside of inner classes, but still usable. – SGM1 Dec 23 '15 at 21:05

5 Answers5

2

You only need to use this if one of your parameters has the same name as one of your members.

i.e.

public class ResultsPage {

    private WebDriver driver;
    private int passenger;

    public ResultsPage(WebDriver driver) {
        // need "this" when we access driver so we are talking about the
        // member and not the parameter. Note the same line uses driver
        // without this to reference the parameter.
        //
        this.driver = driver; 

        // passenger doesn't need this because there is no confusion
        // since there is no parameter or local variable with the
        // name.
        passenger = 10;

        // but you can do it anyway of you think it is clearer...
        this.passenger *= 10; 
    }

Some people use 'this' all the time to make it easier to tell the intent (javac says xyz doesn't exists, but the code is this.xyz so I know it is supposed to be a memeer not a parameter)

Whether that is a good idea is totally opinion based.

John3136
  • 28,809
  • 4
  • 51
  • 69
1

The this keyword is a context. You are basically saying "this" object, has "this" property, which has x value.

From the Java docs:

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.

But to answer your question: The first example is the most common, and consistent use, but either will work because your variable are all instance variables.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
1

Each one does the same thing in this case. However, if you had a constructor with arguments of the same name as your classes member variables then you would have problems if you don't use the "this" keyword. Java will "shadow" the variable names meaning that your variables in your class would not get set, you would just be assigning the passed in variables to themselves and your class variables would remain null.

Look at the code below. In the first constructor the class variables don't get assigned because the passed in variable names are the same and the class variables are said to be shadowed by those names. To tell Java that you mean to assign the value to your class variable and not the passed in variable you have to specify this.

public class ResultsPage {

    private WebDriver driver;
    private JavascriptExecutor jsx;    
    private Selector selector;
    private WaitFor waitFor;
    private WebElementUtilities webElementUtilities;

    //Wrong way!!!
    public ResultsPage(WebElementUtilities webElementUtilities, Selector selector, WaitFor waitFor, JavascriptExecutor jsx) {
        webElementUtilities = webElementUtilities;
        selector = selector;
        waitFor = waitFor;
        jsx = jsx;
    }

    //Right way!!!
    public ResultsPage(WebElementUtilities webElementUtilities, Selector selector, WaitFor waitFor, JavascriptExecutor jsx) {
        this.webElementUtilities = webElementUtilities;
        this.selector = selector;
        this.waitFor = waitFor;
        this.jsx = jsx;
    }
}
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
1

this keyword is a reference to the current object.

Java Docs:

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.

Example:

public class Foo {
    private int bar;

    public Foo(int bar) {
        // the "this" keyword allows you to specify that
        // you mean "this type" and reference the members
        // of this type - in this instance it is allowing
        // you to disambiguate between the private member
        // "bar" and the parameter "bar" passed into the
        // constructor
        this.bar = bar;
    }
}

Check also:

This Keyword Question

Community
  • 1
  • 1
antoniodvr
  • 1,259
  • 1
  • 14
  • 15
0

Both of your constructors are correct. It's just based mostlty on preference.