-2
private static final List<String> datas = new List<String>() {{
    add("aaaa");
    add("bbbb");
    System.out.println(datas);
}};

I have declared a list and added some data. Then I want to print the data stored within that list. But the code does not work. Could you explain why?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
  • 5
    You probably can't do the `System.out.println` inside the declaration -- you could do it in a static initialization block, though. (But the double-brace initialization approach is a dirty hack that you're probably better off avoiding anyway.) – Louis Wasserman Nov 30 '15 at 20:21
  • It does not compile. And it will print `null` if you make it compile. Why do you want to know how this non-working doesn't work? – zapl Nov 30 '15 at 20:25
  • [What is Double Brace initialization in Java](http://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-in-java) ? – Perdomoff Nov 30 '15 at 20:26

3 Answers3

1

You are using here what is refered to as double brace initialization. Basically, this creates an anonymous class with an initializer that does some processing, like adding data to the list.

Written with added line breaks, this is what it really looks like:

private static final List<String> datas = new List<String>() {

    {
        // this is the initializer block
        add("aaaa");
        add("bbbb");
        System.out.println(datas);
    }

    // huh? no methods of List are implemented here!

};

The first problem is that you are trying to create an anonymous class of List but you are not overriding any of its abstract methods. This results in a compilation error.

The second "problem", is that the System.out.println class is inside the initializer, but as this moment, the variable datas is null, so that's will be printed (and that's probably not what you want).

So first, what you want is to create an anonymous class derived from ArrayList, or some other list implementation, so that you don't have to override any methods. Second, you don't want to print the content of the variable inside the initializer, but outside of it. Third, and probably most important: you don't want to use double brace initialization at all!

Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

You need to implement the methods of the java.util.List interface. and your code is not inside a method or static block.

I think it's easier

    // Creating an empty array list
    ArrayList<String> list = new ArrayList<String>();
    // Adding items to arrayList
    list.add("Item1");
    list.add("Item2");
reos
  • 8,766
  • 6
  • 28
  • 34
-1

Your code is implementing List interface as anonymous class, so you have to implement all List methods. I think you had in mind static list initialization that should be done like:

private static final List<String> datas = new ArrayList<String>(); 
static{
    datas.add("aaaa");
    datas.add("bbbb");
    System.out.println(datas);
};
leonz
  • 133
  • 1
  • 4