I’ve been out of the loop on straight Java programming for a while, so this is probably obvious to a lot of people, and has been for a long time. But on the off chance that I’m not the only other idiot out there, I’ll post this.

The old way of instantiating a List in Java was like this:

List strings = Arrays.asList(new String[]{"foo","bar"});

In Java 5, templates were introduced, so I had to change to doing this:

List<String> strings = Arrays.asList(new String[]{"foo","bar"});

It just occurred to me that with variable-length methods, I can simply call asList like this:

List<String> strings = Arrays.asList("foo","bar");

2 thoughts on “Static Java List Instantiation

    • How else would you instantiate a static list? It’s only “bad” if the compiler doesn’t optimize it. Which it does.

      The post demonstrates a way of doing this clearly in Java 5 syntax.

      Reply

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>