rrene
rrene

Reputation: 323

Java alternative code for C# var type

In C#, implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. But in Java, we need to set the data type explicitly. How to write the below code in Java

The following code:

var newPostlink = message.findElement(By.tagName("a"))[0];

Believe, we need to set this as an array type but am struggling with how to write the piece of code

In C#

public static void GoToNewPost() {
var message = Driver.getWebDriver().findElement(By.id("message"));
var newPostlink = message.findElement(By.tagName("a"))[0];
newPostlink.click();

In Java :

public static void GoToNewPost() {
WebElement message = Driver.getWebDriver().findElement(By.id("message"));
var newPostlink = message.findElement(By.tagName("a"))[0];
newPostlink.click();

Upvotes: 1

Views: 1913

Answers (4)

Vojtech Ruzicka
Vojtech Ruzicka

Reputation: 17095

As of Java 10, there is actually Type Inference for local variables, see JEP-286.

You can just write:

var user = new User();

However, you can only use it for local variables and variables in for and for-each loops. No fields or method return values.

Another limitation is that you mus immediately assign a value to the variable, this does not work:

var user;
user = new User();

You cannot assign null though. Note that it is still static typing, compiler just infers the type at compile-time and the resulting bytecode does contain the type information same as if you would declare the type explicitly. This means the performance is not affected as there is no additional processing at runtime.

You can download JDK 10 Early Access build here.

For more details, see this blog post I wrote.

Upvotes: 0

Matt Klein
Matt Klein

Reputation: 8444

I've just found the Lombok val type, which is almost exactly like the C# var keyword, except that it is also declared as final (like C#'s readonly).

Upvotes: 1

dolan
dolan

Reputation: 1804

Java is strongly typed and doesn't have inferred typing. The other answer solves your problem but to your other question, you unfortunately can't get the compiler to infer types for your variables.

Upvotes: -1

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31299

??? newPostlink = message.findElement(By.tagName("a"))[0];

Here you are calling the method findElement on an object of the type WebElement. According to the documentation, this method returns a type WebElement:

WebElement findElement(By by)

Find the first WebElement using the given method.

This is a single object, not an array, so you cannot use the array indexing operator on it in Java, so you cannot put [0] behind it.

Looking at the C# documentation, the situation is exactly the same. So the most likely answer is: you posted the wrong code, and you actually meant to invoke findElements (plural) in that line.

In that case, the Java version returns a List<WebElement> and you can get element zero like this:

WebElement newPostlink = message.findElements(By.tagName("a")).get(0);

But that is a bit pointless, since this is exactly what the findElement method does already: it returns the first of the elements that was found.

So what you really want is to get rid of the [0] and then use WebElement as the type of newPostlink:

WebElement newPostlink = message.findElement(By.tagName("a"));

Upvotes: 1

Related Questions