expected. java

后端 未结 3 616
走了就别回头了
走了就别回头了 2020-12-11 10:49

I have this snippet of java code. I am a noob in java..

Error :

 expected
cfg = new Config;

Code:



        
相关标签:
3条回答
  • 2020-12-11 10:56

    Yes, this is the problem:

    public class ClosureBuilder {
        cfg = new Config();
        ...
    }
    

    At the top level of a class, you can only have:

    • Instance initializer blocks ({ ... })
    • Static initializer blocks (static { ... })
    • Variable declarations
    • Constructor declarations
    • Method declarations
    • Nested type declarations
    • Finalizer declarations

    This is none of these. If you meant to declare a variable, you should have done so:

    private Config cfg = new Config();
    

    If that's not what you intended to do, you should explain your intention.

    EDIT: Once you've fixed that, this compiler error seems pretty clear:

    class Config is public, should be declared in a file named Config.java

    There are two potential fixes for this:

    • Make Config non-public
    • Move it to a file called Config.java

    Either should fix that error (potentially revealing more).

    0 讨论(0)
  • 2020-12-11 11:03

    Though your intention is not very clear, i assume you want to have the cfg created before any other variable. First declare your class Config as non-public or move to file Config.java. It makes sense to initialize cfg in a static block. Below is a possible code snippet:

    private static Config cfg = null;

    private static String JDBC = null;

    static {

      cfg = new Config();
    
      JDBC = cfg.getProperty("JDBC"); 
    

    }

    0 讨论(0)
  • 2020-12-11 11:07

    Where are you declaring your cfg variable?

    I only see the assignment. I think that may be the reason.

    Config cfg = new Config();
    

    Shoud fix it.

    0 讨论(0)
提交回复
热议问题