Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a static final java.net.URL?

My question is simple. I'm trying to make a set of java.net.URLs that are public static final, so that any class can access them from any context, as these URLs won't change during runtime. However, when I try to create them, I get a compiler error telling me that I must catch or declare thrown a java.net.MalformedURLException, but that is impossible outside a method. Is there any way to circumvent such a constructor that throws a non-java.lang Throwable?

Some dummy code below to visualize my problem:

public class Main
{
    public static final java.net.URL STATIC_URL = new java.net.URL("http://example.com/");
    public static void main(String[] args)
    {
        GUI gui = new GUI();
        gui.setVisible(true);
    }
}
public class GUI extends java.awt.Window
{
    public GUI()
    {
        add(new java.awt.Label(Main.STATIC_URL.toString()));
    }
}

If you try to compile this, it will tell you that you can't because of line 3. Hence my question.

like image 551
Ky. Avatar asked Dec 06 '11 07:12

Ky.


People also ask

What is Java net URL in Java?

net. URL. java.lang.Object | +--java.net.URL public final class URL extends Object implements Serializable, Comparable Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web.

How can I get URL in Java?

Constructors of the URL classURL(URL context, String spec): Creates a URL object by parsing the given spec in the given context. URL(String protocol, String host, int port, String file, URLStreamHandler handler): Creates a URL object from the specified protocol, host, port number, file, and handler.

What is URL in advanced Java?

URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory. This section shows you how to write Java programs that communicate with a URL.

What is the function of URL class in Java?

URLConnection Class in Java. The URLConnection class in Java helps to represent a “connection or communication” between the application and the URL. This class also helps us to read and write data to the specified resource of URL.


2 Answers

An "alternative" which I'd prefer to @HosamAly method:

private static final java.net.URL STATIC_URL = makeUrl("http://www.example.com");

public static java.net.URL makeUrl(String urlString) {
    try {
        return new java.net.URL(urlString);
    } catch (java.net.MalformedURLException e) {
        return null; //Or rethrow an unchecked exception
    }
}
like image 161
bezmax Avatar answered Sep 19 '22 00:09

bezmax


Use a static initializer:

public class Main {
    private static final java.net.URL STATIC_URL;
    static {
        java.net.URL temp;
        try {
            temp = new java.net.URL("http://www.example.com");
        } catch (java.net.MalformedURLException e) {
            temp = null;
        }
        STATIC_URL = temp;
    }
}

Note: The usage of a temporary variable is required to avoid a compilation error about assigning to the final static field twice. If the field is not final, the assignment could be done directly.

like image 26
Hosam Aly Avatar answered Sep 23 '22 00:09

Hosam Aly