1

I am newbie to Selenium.

I am writing piece of code DriverManager.Java (to Load Browser)

package com.moni.tef;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverManager {

    private static WebDriver driver;

    static {
        driver = new FirefoxDriver();
    }
    public WebDriver getWebDriver(){
        driver.get("https://public-testing/");
        return driver;
    }
}

and I created another class.. and tries to use this getWebDriver method.. Then my piece of code changes in to public static. Code is running but can any one explain what is this fix (done by eclipse)

public static WebDriver getWebDriver(){
    driver.get("https://xxx.url.url");
    return driver;
}
ChanGan
  • 4,254
  • 11
  • 74
  • 135

2 Answers2

1

IMO, in your newly written class, you tried to call the getWebDriver() method statically:

DriverManager.getWebDriver()

instead of creating an instance of DriverManager firstly:

new DriverManager().getWebDriver()

This would cause a compilation error since initially, DriverManager#getWebDriver() is an instance method, not a class method (thereby static).

Therefore, Eclipse probably helped you (with your involontary approbation surely) by making the method static in order to compile successfully.

For more information about static concept: click here.

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • I guess if i made like this it would not be changed static right????DriverManager driver=new DriverManager(); driver.getWebDriver(); – ChanGan Mar 04 '14 at 02:44
  • Exactly, that's what I guess. You could try again by making a simple code snippet to figure out the Eclipse's behaviour. – Mik378 Mar 04 '14 at 02:44
0

Static members are common for all instances of a class. Non static members are specific to an instance of a class (object). Static methods and members (fields, properties) are used in cases where they don't need to change for the duration of a program's or API's life time. An example of this is Math.PI. A more common (correct) way of describing them is: Static methods and fields are useful when they conceptually don't belong to an instance of something.

Static members also have the property of being accessible from anywhere (unless declared without public) without having an instance (object) of a class.

The special static {} block of a class is run only once when the class is loaded by the JVM.

Community
  • 1
  • 1
  • 2
    > "they don't need to change" => wrong: they can be mutable or even reassigned, so the word "change" doesn't fit. – Mik378 Mar 04 '14 at 02:46
  • @Mik378 you're right. they don't necessarily have to not change to be static. Instead, in case of the variables being static, it means that they won't have different values across instances of the object (i.e they are not instance variables). In the case of methods being static, it means that these methods can be called even without creating an instance of the class (by calling ClassName.method() ). Additionally, static methods are allowed to use static variables and other static methods of the class. – josephus Mar 04 '14 at 03:17
  • @Josephus If I want to be very strict: You wrote: "it means that they won't have different values across instances of the object" => wrong, in case where some distinct threads share a variable (`static` or not) without being read enclosed in `synchronized` block or without `volatile` keyword ;) The thread's cache may be different from the main memory. – Mik378 Mar 04 '14 at 03:21
  • @JosephusVillarey and instance methods are not allowed to use static members? :) – Ярослав Рахматуллин Mar 04 '14 at 03:22
  • "Static members are common for all instances of a class" strictly speaking is true, but it is quite misleading. The truth is. `static` is irrelevant to instances (as instances of a class are not required for static access) and shouldn't be thought of / explained in terms of objects/instances. – ryvantage Mar 04 '14 at 03:38
  • 1
    @Mik378 right again, i forgot about threads and caching. good point! – josephus Mar 04 '14 at 04:03
  • 1
    @Mik378 apparently I forgot to add "only" -- as in static methods are ONLY allowed to use static variables and other static methods of the class. – josephus Mar 04 '14 at 04:10