Q:

initialize a webdriver instance

Hi i have a global class as belows:
public class Global 
{
    public WebDriver driver=null;
    public WebDriverWait wait=null;

    public Global()
    {
        BrowserInitializer obj=new BrowserInitializer();
        obj.initialize();
    }
}

I have another class called browser initializer where i want to initialize the driver instance to firefox as below:
public class BrowserInitializer extends Global 
{
    public void initialize()
    {
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        wait=new WebDriverWait(driver, 5000);
    }
}

I have testNG class where i want to run some test as belows :
public class TestNG1
{
Global globalObj=new Global();
    @Test
    public void login()
    {
        globalObj.driver.get("someURL");
        globalObj.driver.findElement(By.id("someid")).sendKeys("someusername");
        globalObj.driver.findElement(By.id("someid")).sendKeys("somepassword");
        globalObj.driver.findElement(By.id("someid")).submit();
    }
}

Now i am getting error like :
org.testng.TestNGException: 
Cannot instantiate class unitTest.myTest.TestNG1
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:577)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.StackOverflowError
    at unitTest.myTest.Global.<init>(Global.java:12)
    at unitTest.myTest.BrowserInitializer.<init>(BrowserInitializer.java:6)
    at unitTest.myTest.Global.<init>(Global.java:14)

but when i am initializing the driver instance in the constructor of the Global class i am able to run my test. what seems to be the problem? am i going wrong somewhere in the concepts of java?

A:

You have infinite recursion when calling Global's constructor. Your initialize() is perfectly fine.
 public Global()
    {
        BrowserInitializer obj=new BrowserInitializer();
        obj.initialize();
    }

BrowserInitializer is a Global.
So Global's contructor calls BrowserInitializer's constructor (which calls super's i.e  Global's constructor again)
Use something like:
public class BrowserInitializer extends Global 
{
     BrowserInitializer(){
     super();
     this.initialize();
    }

    public void initialize()
    {
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        wait=new WebDriverWait(driver, 5000);
    }
}

//In test class
Global globalObj=new BrowserInitializer();

