C# => JAVA: Filling a static ArrayList on declaration. Possible?

12,618

Solution 1

I don't understand what you mean by

able to access the values without creating a instance of the class

but the following snippet of code in Java has pretty much the same effect in Java as yours:

private static List<String> inputs = Arrays.asList("Foo", "Bar", "Foo2", "Bar2");

Solution 2

You can use Double Brace Initialization. It looks like:

private static List<String> inputs = new ArrayList<String>()
  {{ add("Foo");
    add("Bar");
    add("Foo2");
    add("Bar2");
  }};

Solution 3

You can make static calls by enclosing them within static{} brackets like such:

private static final List<String> inputs = new ArrayList<String>();

static {
  inputs.add("Foo");
  inputs.add("Bar");
  inputs.add("Foo2");
  inputs.add("Bar2");
}

Solution 4

Do you need this to be an ArrayList specifically, or just a list?

Former:

private static java.util.List<String> inputs = new java.util.ArrayList<String>(
    java.util.Arrays.<String>asList("Foo", "Bar", "Foo2", "Bar2"));

Latter:

private static java.util.List<String> inputs =
    java.util.Arrays.<String>asList("Foo", "Bar", "Foo2", "Bar2");

java.util.Arrays#asList(...) API

Solution 5

You may enjoy ImmutableList from Guava:

ImmutableList<String> inputs = ImmutableList.of("Foo", "Bar", "Foo2", "Bar2");

The first half of this youtube video discusses the immutable collections in great detail.

Share:
12,618
Peterdk
Author by

Peterdk

Indie developer of mobile apps at Umito. Android apps are main focus. Top apps are: Mini Piano Lite (1.4 million+), Fretter and KeyChord. The latter one has been featured on Google Play. Also co-dev of Addons Detector (1 million+) Experience: Java, Swift, Ruby, Objective-C, C# Learning: Kotlin and Rust. Minor experience: C, C++

Updated on June 25, 2022

Comments

  • Peterdk
    Peterdk almost 2 years

    In my C# project I have a static List that gets filled immediately when declared.

      private static List<String> inputs = new List<String>()
            { "Foo", "Bar", "Foo2", "Bar2"};
    

    How would I do this in Java using the ArrayList?

    I need to be able to access the values without creating a instance of the class. Is it possible?

  • Brett
    Brett over 14 years
    As it is a static field, it really should be immutable. Unfortunately this adds to the verbosity. private static final List<String> inputs = Collections.unmodifiableList(Arrays.asList("Foo", "Bar", "Foo2", "Bar2"));. Should get list literals in JDK7.
  • jdmichal
    jdmichal over 14 years
    Yay for the less verbose, later answer winning out? (No offense to you MAK. Just stating my perceived truth.)
  • Adriaan Koster
    Adriaan Koster over 14 years
    IMO This is the best answer, since it doesn't rely on helper class java.util.Arrays.
  • jdmichal
    jdmichal over 14 years
    But it does create a somewhat hidden anonymous inner class. Subjective call on whether or not that is a better tradeoff.
  • MAK
    MAK over 14 years
    @jdmichal: I think your answer was posted while I was still writing mine - otherwise I wouldn't have bothered to say the same thing again. I guess the slowest gun won :).
  • Peterdk
    Peterdk over 14 years
    That's nice, I didn't know that.
  • Leo Izen
    Leo Izen over 10 years
    I often like to run new ArrayList<>(Arrays.asList(...)) because Arrays.asList() doesn't produce a full implementation of List but ArrayList does. It's slightly slower if this has to be done repeatedly but if it's a one-shot deal then it's fine.