Why does Eclipse require me to set (arbitrary) brackets in java code?
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.
Basically, I have been haunted by strange Eclipse error messages. I tracked the last one down to this problem:
This works:
public class CoordinationGame extends Scape {
.
.
.
Scape lattice;
boolean test;
int test2;
{
test = true;
test2 = 3;
}
{
lattice = new Scape(new Array2DVonNeumann());
}
}
This gives strange error messages:
public class CoordinationGame extends Scape {
.
.
.
Scape latt开发者_Python百科ice;
boolean test;
int test2;
test = true;
test2 = 3;
lattice = new Scape(new Array2DVonNeumann());
}
i.e. {
expected afer int test2
and Syntax error on token "lattice", VariableDeclaratorId expected after this token
.
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.
Does anyone know why Eclipse requires me to set these brackets, or what I could do to change this behaviour?
Thanks in advance! Martin
PS: Some information that will probably be useful:
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 …
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();}
This is an instance initializer, it will be copied into all constructors by the compiler.
(See Java Tutorial > Initializing Fields)
In Java, you can write statements in
- a method
- a constructor (which is a special kind of method)
- an initializer block (static or instance)
but nowhere else.
If you separate field initialization from declaration, you need a method or an initializer. This instance works without initializers:
package ede.brook.model;
import org.ascape.model.Scape;
public class CoordinationGame extends Scape {
public int latticeHeight = 30;
public int latticeWitdh = 30;
public int nPlayers = 200;
Scape lattice = new Scape(new Array2DVonNeumann());;
Scape players;
boolean test = true;
int test2 = 3;
test = true;
test2 = 3;
}
If an initializer is present, they are executed before the constructors.
As for coding practice, I would recommend against initializers and use a combined declaration + initialization for simple cases or (parameterless) constructors for more complicated constructs. An exception are static initializers, which may be necessary for more complex initializations:
static SomeTypeWithComplexInitialization staticField;
static {
SomeOtherType factoryParameter = new SomeOtherType()
staticField = SomeTypeFactory.createInstance(factoryParameter);
}
The only other instance where I would recommend using initializers are APIs that specifically recommend this. For example, JMock uses this syntax to provide an easy-to-grock lambda-like construct:
context.checking(new Expectations() {{
oneOf (subscriber).receive(message);
}});
public class CoordinationGame extends Scape {
// ...
int test2;
// ...
test2 = 3; // <- errror
}
This is illegal, we can't assign values this way in a class body. The other code was legal because you used a static initializer to init the field test2
.
The error message is pretty misleading but, honestly, that happens quite often in Java ;)
If you want to initialize the test
and test2
variables you should do so at the point of declaration.
boolean test = true;
int test2 = 3;
The {
and }
you have added make an initializer block which avoids the error. It's a bit of an odd way to do things, so I'd always prefer to see variables initialized at the point of declaration if that's possible!
If you want to initialize the fields lattice, test and test2, try using the following:
package ede.brook.model;
import org.ascape.model.Scape;
public class CoordinationGame extends Scape {
private static final long serialVersionUID = 1L;
public int latticeHeight = 30;
public int latticeWitdh = 30;
public int nPlayers = 200;
Scape lattice = new Scape(new Array2DVonNeumann());
Scape players;
boolean test = true;
int test2 = 3;
}
精彩评论