简体   繁体   中英

Eclipse proposals(ctrl+Space) needs to be in curly braces

import java.sql.Driver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class test {

    //{

    WebDriver driver = new FirefoxDriver();
    driver.

    //}
}

When I am commenting curly braces I am not getting proposals(ctrl+Space) after "driver.", while uncommenting curly braces gives proposals(ctrl+Space). Wasted many hours to get this solution, please help me out.

This is because it isn't legal syntactically to put a statement inside a class without enclosing it with braces or putting it inside a method.

Nothing strange going on here, just Java syntax rules.

Your code snipper is not part of a method. So without curly braces you are in Declare Member Mode . But, while declaring members you cannot invoke any methods on objects (or query any fields).

When placing the code snippet inside the curly braces, you are in fact inside an initializer block , where such code is allowed.

Eclipse knows that, and correctly does not provide you any proposals in the first scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM