简体   繁体   English

<identifier> 预期。 java的

[英]<identifier> expected. java

I have this snippet of java code. 我有这段java代码。 I am a noob in java.. 我是java中的菜鸟..

Error : 错误:

<identifier> expected
cfg = new Config;

Code: 码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.io.*; 

import java.util.*;
import java.util.Properties;

public class Config 
{

   Properties configFile;
  public Config()
{
configFile = new java.util.Properties();
try {           
  configFile.load(this.getClass().getClassLoader().getResourceAsStream("config"));          
}catch(Exception eta){
    eta.printStackTrace();
}
  }

  public String getProperty(String key)
  {
 String value = this.configFile.getProperty(key);       
    return value;
  }

}



public class ClosureBuilder {

cfg = new Config();

private static String JDBC = cfg.getProperty("JDBC");
private static String URL = cfg.getProperty("URL");
private static String DIMENSION_TABLE = cfg.getProperty("DIMENSION_TABLE");
private static String CLOSURE_TABLE = cfg.getProperty("CLOSURE_TABLE");
private static String KEY = cfg.getProperty("KEY");
private static String PARENT_KEY = cfg.getProperty("PARENT_KEY");

private static Object TOP_LEVEL_PARENT_KEY = '0';


private Object topLevel = null;

private Set<Object> processedNodes;

private PreparedStatement aPst;
public static void main(String[] args) throws Exception {

----------- More code --------

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 { ... } ) 静态初始化块( 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 class Config是public,应该在名为Config.java的文件中声明

There are two potential fixes for this: 有两种可能的解决方法:

  • Make Config non-public 使Config非公开
  • Move it to a file called Config.java 将其移动到名为Config.java的文件

Either should fix that error (potentially revealing more). 要么应该修复该错误(可能揭示更多)。

Where are you declaring your cfg variable? 你在哪里宣布你的cfg变量?

I only see the assignment. 我只看到作业。 I think that may be the reason. 我想这可能就是原因。

Config cfg = new Config();

Shoud fix it. 大声解决它。

Though your intention is not very clear, i assume you want to have the cfg created before any other variable. 虽然你的意图不是很明确,但我假设你想在任何其他变量之前创建cfg。 First declare your class Config as non-public or move to file Config.java. 首先将您的类Config声明为非公共或移动到文件Config.java。 It makes sense to initialize cfg in a static block. 在静态块中初始化cfg是有意义的。 Below is a possible code snippet: 以下是可能的代码段:

private static Config cfg = null; private static Config cfg = null;

private static String JDBC = null; private static String JDBC = null;

static { 静态的 {

  cfg = new Config();

  JDBC = cfg.getProperty("JDBC"); 

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM