1

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)"

3 Answers 3

1

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);
}
2
  • Ok, re-writing the method as you have here solves the problem. So the "main" method should not be used unless it is the programs entry point right? It is used only once in a program and what we do here is create a constructor that accepts an argument from anywhere in the program to give the class the information it needs? Thanks for your help!!! Nov 19, 2014 at 16:52
  • Well yes, main methods are typically your single entry point (although they can be invoked just like any other methods too). A constructor is a special construct used to initialize your classes through the new keyword. You can parametrize your constructors with whichever arguments you need, and overload (and override) constructors.
    – Mena
    Nov 19, 2014 at 16:56
1

Java adds a default constructor to every class that doesn't provide one. A constructor is a method without a return type. So, in your case the default constructor DownloadHandler() is automatically added to your class and it does not take any parameters while you are trying to initialize it with a String.

The String you are using in main method right now is coming from console from user.

1

From your code its obvious that you want to pass a argument via command line parameter. But when you are initiating DownloadHandler, you are passing that string here which is not you should be doing.

There are two things you can do now.

  1. Pass the string via command line parameter java DownloadHandler yourstring

  2. Write a constructor which accepts the string. In your code outside of your main method

    String url;

    public DownloadHandler(String str)

    {

    url = str;

    }

Now call

d.URLSetup(url);

Hope this will clear your doubts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.