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!