how to initialize static ArrayList in one line

You can use double braces initialization: –

List<MyClass> list = new ArrayList<MyClass>() {
    {
        add(new MyClass("name", "address", 23));
        add(new MyClass("name2", "address2", 45));
    }
};

As you can see that, inner braces is just like an initializer block, which is used to initialize the list in one go..

Also note the semi-colon at the end of your double-braces

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)