Search code examples
javaclassmavenintellij-ideaebean

Cannot create new Object from Maven dependency


I'm trying to make an Ebean ServerConfig as explained here: http://www.avaje.org/ebean/getstarted_programmatic.html

But, when in my project I create a new ServerConfig object, I cannot access the methods in it.

package controller;

import com.avaje.ebean.config.ServerConfig;

public class ormConfig {
    ServerConfig config = new ServerConfig();
    config.setName("mysql");
}

No expection, no hint from the IDE. Only an error from the compiler:

"Error:(14, 19) java: <identifier> expected"

https://gist.github.com/Szil/f65bc2d7180d2ae49ad5

Included the pom.xml in the Gist.

I have no idea where the problem is. A bit newbie in Maven and not exactly expert in Java, but simply creating a new object shouldn't be a problem most of the time.


Solution

  • You can't have arbitrary code directly inside a class. Only fields and methods declarations. Code like

     config.setName("mysql");
    

    must go into a method or a constructor.

    Also, classes conventionally start with an uppercase letter in Java:

    public class OrmConfig {
        ServerConfig config = new ServerConfig();
    
        OrmConfig() {
            config.setName("mysql");
        }
    }