Java ArrayList add() method in the instance variable section

情到浓时终转凉″ 提交于 2019-11-28 06:03:28

问题


In a Java class where you normally declare/define instance variables, I would like to have an ArrayList as one of the instance variables and initialize it with some elements to start out with. One way of doing this is declare the ArrayList and initialize it in a constructor. However, I am wondering why it is illegal to initialize the value outside the constructor. For example,

public class Test {
    // some instance variables...

    private ArrayList<String> list = new ArrayList<String>();
    list.add("asdf");

    // methods here...
}

So I get that this is illegal. But why exactly is this illegal?


回答1:


You cannot execute statements freely in a class. They should be inside a method. I recommend you to add this line in the constructor of the class or in a class initialization block.

In class constructor:

public class Test {
    // some instance variables...

    private List<String> list = new ArrayList<>();

    public Test() {
        list.add("asdf");
    }
    // methods here...
}

In class initialization block:

public class Test {
    // some instance variables...

    private List<String> list = new ArrayList<>();

    {
        list.add("asdf");
    }
    // methods here...
}

More info:

  • What's the difference between an instance initializer and a constructor?



回答2:


It is illegal, because initializing fields is exactly the reason, why constructors exist!

In Java, it is not possible to have code anywhere 'outside' of a method or constructor (or class-initializer). It is possible to have simple expressions in a field initializer, but not multiple statements:

public class Test {
    // here, at class level, you can only declare fields, methods or constructors!
    // But you cannot have procedural code!!!

    // field without initializer (=> default initialization with null)
    private List<String> list1;

    // field with explicit initializer expression
    private List<String> list2 = new ArrayList<String>();

    public Test() {
        // initialize fields
        this.list1 = new ArrayList<>();

        this.list2.add("asdf");
    }
}



回答3:


If you use Guava (https://code.google.com/p/guava-libraries/) you will have this sugar code available:

private ArrayList<String> list = Lists.newArrayList("element1", "element2", ...)

Also, has was mentioned before, I suggest not typing your field as ArrayList but as List. Always use the more abstract type, good rule of the thumb.




回答4:


That code will not get called if it outside a defined function for the class. If this were to be allowed, everything within the class would get executed as soon as you created a class which is not a behavior one would want.




回答5:


If you would like to initialize with some elements, you can do it like this:

private ArrayList<String> list = new ArrayList<>(java.util.Arrays.asList("asdf"));



回答6:


You cannot do what you're proposing, instead you can initialize the variable using java.utils.Arrays.asList(T) like this:

public class Test{
    private List<String> foo = new ArrayLis<>(Arrays.asList("a", "b", "c"));
}


来源:https://stackoverflow.com/questions/29681431/java-arraylist-add-method-in-the-instance-variable-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!