Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redeclaration of parameters

While looking through the Selenium source code I noticed the following in the 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 benefit of having the following line?

final WebDriver driverRef = driver;

Wouldn't it have made sense to just make the parameter final, and then passing that along to the next method without declaring the new reference?

like image 493
Scott Avatar asked Jun 11 '12 16:06

Scott


2 Answers

Well, the answer is that setting final on a variable and only use it as an argument to a function is completely useless. In the DefaultElementLocatorFactory constructor, the variable related to the input argument can be freely reassigned, since it is a copy of the original reference.

P.S. ... unless of course, as suggested by the OP, the input argument is instead declared final.

like image 175
Luca Geretti Avatar answered Nov 13 '22 20:11

Luca Geretti


The best thing I can come up with (under the assumption that the selene devs have more than a basic understanding of how java works - which I think is given):

Presumably before there was a DefaultElementLocatorFactory class, the method used an anonymous inner function and when the code was refactored some parts were just overlooked.

like image 39
Voo Avatar answered Nov 13 '22 20:11

Voo