KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
My question is simple. I'm trying to make a set of java.net.URL s 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.
Tags (comma-separated)
Save Edits
Cancel