Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Avoid reflection whenever possible. It often does more harm than good.</p> <p>Put them into a map:</p> <pre><code>public class Urls { //put your arrays here private static final Map&lt;String,String[]&gt; urlsLists = new HashMap&lt;&gt;(); static { urlLists.put("ASTUN", ASTUN); urlLists.put("CANDANCHU", CANDANCHU); } public static String[] getUrlList(String name) { return urlLists.get(name); } } </code></pre> <p>And then call it like this:</p> <pre><code>private Object getObject(String objectName){ return Urls.getUrlList(objectName); } </code></pre> <p><strong>Update</strong></p> <p>You loose a lot of nice stuff Java help you with, including type-safety, encapsulation and compile-time checks. Because of this it is a lot more error prone. There is a much increased risk of run-time errors and you need a bunch of extra code to handle this. Your brief example already have two catch clauses. Trust me - that will just get worse.</p> <p>You can even improve type-safety more by creating an Enum to define url-types. Then you will get compile time checks that you have spelled the name right and even auto-completion all through-out your code. :)</p> <pre><code>public class Urls { public enum UrlTypes {ASTUN; CANDANCHU;} // .. private static final Map&lt;UrlTypes,String[]&gt; urlsLists = new HashMap&lt;&gt;(); static { urlLists.put(UrlTypes.ASTUN, ASTUN); urlLists.put(UrlTypes.CANDANCHU, CANDANCHU); } .. public static String[] getUrlList(UrlTypes name) { return urlLists.get(name); } } </code></pre> <p>Every error you can catch at compile-time instead of at run-time can save you between half an hour or half a week of work, when things get complex.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload