Search code examples
javaclassmethodsparent

I don't understand the call to the parent class from within the class in this JAVA class


Can you help me understand why we call the parent class here? I found a download class that seemed simple enough but could use help wrapping my brain around the first method.

public class DownloadHandler {

        public static void main(String[] args) throws Exception {

            DownloadHandler d = new DownloadHandler();
            d.URLSetUp(args[0]);
        }
....
}

I am trying to instantiate the handler in a for loop and getting an error.

DownloadHandler file = new DownloadHandler("http://example.com/"+cleanLink+"/"+filename+".pdf")

It says "DownloadHandler() in DownloadHandler cannot be applied to (java.lang.String)"


Solution

  • Your DownloadHandler class has a static void main method, which is the single point of entry when executing command-line programs.

    That method is not a constructor.

    What it does is initialize a new instance of DownloadHandler and invoking an instance method on that object by passing the given String argument.

    Not sure what's the usage there.

    In order for your initialization to compile, you probably want to add a constructor that performs similar operations, given a single String parameter in your case.

    For instance:

    public DownloadHandler(String s) {
        URLSetUp(s);
    }