[英]Java Help: why can't I access the static methods declared in a class outside the current main method file
Following the JavaDocs tutorial for SAX: The method usage() is not recognised from the main method. 按照SAX的JavaDocs教程进行操作:从主方法中无法识别方法use()。 As far as I know it should be accessible as the methods are declared as static and exist within the same package as the main method. 据我所知,它应该是可访问的,因为方法被声明为静态的,并且与main方法位于同一包中。
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() appears to be a member of the SAXLocalNameCount class, not the Main class. usage()似乎是SAXLocalNameCount类的成员,而不是Main类的成员。 Despite the indentation. 尽管有缩进。
The usage method as is defined now can only be used from inside another static method or static code block of SAXLocalNameCount class. 现在定义的用法方法只能在SAXLocalNameCount类的另一个静态方法或静态代码块内部使用。
You can: 您可以:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.