为什么Eclipse需要我在java代码中设置(任意)括号? [英] Why does Eclipse require me to set (arbitrary) brackets in java code?

查看:176
本文介绍了为什么Eclipse需要我在java代码中设置(任意)括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图找出如何使用Eclipse在java中编程Escape模型。我是Escape和Eclipse的新手,自从我在java编程以来已经有一段时间了,所以请原谅,如果这是一个愚蠢的问题。

I am currently trying to figure out how to use Eclipse to program Escape models in java. I am quite new to Escape and Eclipse, and it has been a while since I programmed in java, so please excuse if this is a stupid question.

基本上,我一直困扰着奇怪的Eclipse错误消息。我跟踪了最后一个这个问题:

Basically, I have been haunted by strange Eclipse error messages. I tracked the last one down to this problem:

这样做:

public class CoordinationGame extends Scape {

.
    .
    .
    Scape lattice;

boolean test;
int test2;

{
test = true;
test2 = 3;
}

{
lattice = new Scape(new Array2DVonNeumann());
}

}

这会给出奇怪的错误信息: / p>

This gives strange error messages:

public class CoordinationGame extends Scape {

    .
.
.

Scape lattice;

boolean test;
int test2;

test = true;
test2 = 3;

lattice = new Scape(new Array2DVonNeumann());
}

{期望afer int test2 令牌lattice上的语法错误,之后的VariableDeclaratorId令牌

正如我所说,Java已经有一段时间了,但是IIRC不应该需要这些括号。 此问题确定这个括号使用是一个初始化块,但这可能不是我想在这里做的。

As I said, Java has been some time, but IIRC, those brackets should not be required. This question establishes that this bracket usage is an initializing block, but this is probably not what I want to do here.

有谁知道为什么Eclipse需要我设置这些括号,我可以做些什么来改变这种行为?

Does anyone know why Eclipse requires me to set these brackets, or what I could do to change this behaviour?

提前感谢!
Martin

Thanks in advance! Martin

PS:可能有用的一些信息:

PS: Some information that will probably be useful:

我使用eclipse-indigo ,安装了建模工具包AMP。我不完全确定如何检查这个,但我相信我正在使用jre6,因为这是我的包资源管理器的JRE系统库选项卡中所说的。在未来,我打算找到一种使用Groovy运行Escape建模环境的方法,但这将是另一个问题。

I use eclipse-indigo, installed the modelling toolkit AMP. I am not entirely sure how to check this, but I believe I am using jre6, as this is what it says in the JRE System Library tab of my package explorer. In the future, I plan to find a way to run the Escape modelling environment with Groovy, but that will be another question …

推荐答案

这与您的IDE无关。 Java不允许在类级别的语句,但它允许在类级别的初始化器。

This has nothing to do with your IDE. Java does not allow statements at class level, it does however allow initializers at class level.

{foo();}

这是一个实例初始化程序,它将被编译器复制到所有构造函数中。

(请参阅 Java教程>初始化字段

This is an instance initializer, it will be copied into all constructors by the compiler.
(See Java Tutorial > Initializing Fields)

在Java中,您可以在

In Java, you can write statements in


  • 中写一个方法

  • 一个构造函数(这是一种特殊的方法)

  • 一个初始化程序块(静态或实例)

,但没有其他地方。

这篇关于为什么Eclipse需要我在java代码中设置(任意)括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆