简体   繁体   English

Java帮助:为什么我不能访问当前主方法文件之外的类中声明的静态方法

[英]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: 您可以:

  1. Move the static main method inside the SAXLocalNameCount class and out of the Main class. 将静态main方法移到SAXLocalNameCount类内,然后移出Main类。
  2. Change the visibility of usage method to public and than use it from the Main class -> static main method. 将用法的可见性更改为public,然后从Main class-> static main方法使用它。

暂无
暂无

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

相关问题 为什么不能将 Java 类声明为静态? - Why can't a Java class be declared as static? 为什么我不能在声明方法之外访问本地类? - Why can't I access local class outside declaring method? “方法 main 不能声明为静态; 静态方法只能在静态或顶级类型中声明” - “The method main cannot be declared static; static methods can only be declared in a static or top level type” 方法main不能声明为静态; 静态方法只能以静态或顶级类型声明 - The method main cannot be declared static; static methods can only be declared in a static or top level type 为什么访问说明符不能用于Java类中方法内声明的变量? - Why access specifiers can't be used for variables declared inside method in a Java Class? 为什么我们不能在 java 的子 class 的方法之外访问 class 内部的父实例变量 class? - why can't we access parent class instance variables inside the class outside the method in child class in java? 为什么我可以在main中使用外部非静态类和方法,而不能使用类中定义的类和方法? - Why can I use external non static classes and methods inside main, and can't with classes and methods defined inside the class? Java:使用 Static 方法在 Main 之外的方法中调用 Object - Java: Calling Object in Method Outside of Main with Static Methods 为什么我不能使用get方法访问另一个类中声明的TreeMap - Why can't I access TreeMap declared in a class from another with get method 为什么我不能从Scala访问实现Java接口的类中声明的变量? - Why can't I access a variable declared in a class, which implements a Java interface, from Scala?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM