遵循 SAX 的 JavaDocs 教程: 主方法无法识别方法usage()。据我所知,它应该是可访问的,因为这些方法被声明为静态,并且与主方法存在于同一包中。
public class Main {
public static void main(String args[]){
String filename = null;
//Checks to see if commnad line arguments are present
for (int i = 0; i < args.length; i++) {
filename = args[i];
if (i != args.length - 1) {
usage();
}
}
if (filename == null) {
usage();
}
//Defined in the same package as the main method
public class SAXLocalNameCount extends DefaultHandler{
private Hashtable tags;
public void startDocument() throws SAXException{
tags = new Hashtable();
}
//The problem method
private static void usage() {
System.err.println("Usage: SAXLocalNameCount <file.xml>");
System.err.println(" -usage or -help = this message");
System.exit(1);
}
}
最佳答案
usage() 似乎是 SAXLocalNameCount 类的成员,而不是 Main 类的成员。尽管有缩进。
关于Java帮助: why can't I access the static methods declared in a class outside the current main method file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399639/