Parameter Override

While looking at the Selenium source code, I noticed the following in PageFactory:

public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy) { T page = instantiatePage(driver, pageClassToProxy); initElements(driver, page); return page; } public static void initElements(WebDriver driver, Object page) { final WebDriver driverRef = driver; initElements(new DefaultElementLocatorFactory(driverRef), page); } 

What is the advantage of the next line?

 final WebDriver driverRef = driver; 

Doesn't it make sense to just make the final parameter, and then pass this to the next method without declaring a new link?

+6
source share
2 answers

Well, the answer is that setting final for a variable and using it only as an argument to a function is completely useless. In the constructor of DefaultElementLocatorFactory variable associated with the input argument can be freely reassigned, since it is a copy of the original link.

PS ... unless, of course, as suggested by the OP, the input argument final declared instead.

+3
source

The best I can come up with (assuming selene devs has more than a basic understanding of how java works - which seems to me given):

Presumably, before the DefaultElementLocatorFactory class DefaultElementLocatorFactory , the method used an anonymous internal function, and when the code was reorganized, some parts were simply left out.

+2
source

Source: https://habr.com/ru/post/917812/


All Articles