Why does Eclipse require me to set (arbitrary) brackets in java code?

后端 未结 5 1201
-上瘾入骨i
-上瘾入骨i 2021-01-29 11:56

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

相关标签:
5条回答
  • 2021-01-29 12:03

    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.

    0 讨论(0)
  • 2021-01-29 12:05
    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 ;)

    0 讨论(0)
  • 2021-01-29 12:10

    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);
    }});
    
    0 讨论(0)
  • 2021-01-29 12:17

    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!

    0 讨论(0)
  • 2021-01-29 12:17

    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;
    
        }
    
    0 讨论(0)
提交回复
热议问题