rrene
rrene

Reputation: 323

Convert "auto property" in C# to Java alternative code

I am new to Java and currently trying to follow along a "Selenium Automation Framework" course. Unfortunately, the tutorial is in C#. I got stuck with a piece of code in C# and not able to convert it to Java alternative code. From my understanding,

public static IWebdriver Instance { get; set; } is a auto property which is not available in Java. Any suggestions, greatly appreciated?

package WordpressFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Driver {

public static IWebdriver Instance { get; set}

    public static void Initialize()
    {
        WebDriver Instance;
        Instance=new FirefoxDriver();
        Instance.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
     }
}

Upvotes: 2

Views: 611

Answers (4)

parishodak
parishodak

Reputation: 4676

IWebDriver interface is defined as WebDriver in Java.

public class Driver {

    private Webdriver webDriver;

    public static Webdriver getWebDriver() {
        return webDriver;
    }

    public static void setWebDriver(Webdriver webDriver) {
        this.webDriver = webDriver;
    }
}

In eclipse you just need to declare a private variable and use eclipse's code generation capabilities to generate getters and setters.

Right click -> Source -> Generate setters and getters

Reference: Is there a way to automatically generate getters and setters in Eclipse?

Upvotes: 1

Vadim Martynov
Vadim Martynov

Reputation: 8902

Java has no properties then you can use get/set methods. Also it'll be better to change name from nonsensical "instance" to "webDriver".

private static IWebdriver webDriver;

public static IWebdriver getWebDriver() {
    return webDriver;
}

public static void setWebDriver(IWebdriver webDriver) {
    Driver.webDriver = webDriver;
}

Upvotes: 2

OPK
OPK

Reputation: 4180

This piece of C# code:

public static IWebdriver Instance { get; set}

Is a simplified way of declaring variable and set getter and setter compare to Java.

Upvotes: 0

Ian
Ian

Reputation: 1504

Drop the get/set, and just leave: public static IWebdriver Instance;

It will be a member variable instead of a "property".

Upvotes: 0

Related Questions