0

Following the JavaDocs tutorial for SAX: The method usage() is not recognised from the main method. 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.

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);
    }
}
  • For private methods it's the same class. For the same package is not modified (i.e. non-public, non-private, non-protected). – Pedro Montoto García Feb 08 '15 at 21:37
  • `usage()` is defined within the `SAXLocalNameCount` class. To use it you would have to write `SAXLocalNameCount.usage();` or as Sotirios points out, use an `import static`. Also, that method is private so only `SAXLocalNameCount` can use it anyway. – takendarkk Feb 08 '15 at 21:37
  • Use a static import. – Sotirios Delimanolis Feb 08 '15 at 21:37
  • using a static import returns this compiler error. The static import crimeReporter.SAXLocalNameCount must be a field or member type – Gary Cassar Feb 08 '15 at 21:43
  • Add another `.` to the end of that then list the member you want to import. Like `crimeReporter.SAXLocalNameCount.usage;` Regular imports get the whole class, static imports only get a static member from inside the class. (member being a variable or method) – takendarkk Feb 08 '15 at 22:12

2 Answers2

2

usage() appears to be a member of the SAXLocalNameCount class, not the Main class. Despite the indentation.

Shripathi Kamath
  • 310
  • 3
  • 10
  • Thanks for the response, I know I could fix the error by migrating the main method to the SAXLocalNameCount class, but is there any way to access the static methods in the SAXLocalNameCount, without creating an instance of it? update: I tried to make a static import but got this error message fromthe compiler "The static import crimeReporter.SAXLocalNameCount must be a field or member type" – Gary Cassar Feb 08 '15 at 21:48
  • Given that as written, nothing in the `usage()` method accesses or modifies the state of the `SAXLocalNameCount` class, consider making it public. – Shripathi Kamath Feb 08 '15 at 22:35
0

The usage method as is defined now can only be used from inside another static method or static code block of SAXLocalNameCount class.

You can:

  1. Move the static main method inside the SAXLocalNameCount class and out of the Main class.
  2. Change the visibility of usage method to public and than use it from the Main class -> static main method.
MrSoze
  • 1