简体   繁体   English

Eclipse建议(Ctrl + Space)必须放在花括号中

[英]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). 当我在大括号中注释时,在“ driver。”之后没有得到提案(ctrl + Space),而在未注释大括号的情况下却给出了提案(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. 这里没有什么奇怪的,只是Java语法规则。

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. Eclipse知道这一点,并且在第一种情况下正确地没有为您提供任何建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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