1

When I try to instantiate an object from the URL class like this:

URL myURL = new URL("http://example.com");

the compiler arise and error:

error: constructor URL in class URL cannot be applied to given types;

and points to the key work 'new' in the line above. The all class is:

import java.net.*;
import java.io.*;

public class URL {
  public static void main(String[] args) {

    URL myURL = new URL("http://example.com");

    String protocl = myURL.getProtocol();

    System.out.println(protocol);
  }
}

plz help !!

1
  • 1
    It's a conflict between the name of your class, and the java.net.URL class that you imported. Don't name your class URL. Commented Jul 9, 2015 at 1:33

1 Answer 1

5

Your class name is URL but you are probably trying to instantiate a new java.net.URL object. Since you didn't specifically say which URL object you were creating the compiler assumed it was your object and it doesn't have a constructor that takes a String. Rename your class to something else and the problem should go away.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.