What will be the result of compiling and running the following code:

import java.util.List;
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        List<String> values = new ArrayList<String>() {
            {
              add("one");
              add("two");
              add("three");
            }
        };
        System.out.print("values: ");   
        for (String value : values) {
            System.out.print(value + " ");
        }
    }
}
Explanation
An anonymous class is inherited from ArrayList, the constructor ArrayList and the initializer of the anonymous class are called, constructor of the anonymous class is called

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback