Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

<identifier> expected. java

I have this snippet of java code. I am a noob in 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 --------
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...