_
_
_umr2017-02-09 19:22:59
Java
_umr, 2017-02-09 19:22:59

Why can't ArrayList be used outside of a function?

Why can't ArrayList be used outside of a function? I understand that this makes little sense, but the question is ripe

import java.util.*;

public class Tmp {

    ArrayList<Integer> LI = new ArrayList<Integer>();
    LI.add(1231);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Frozen Coder, 2017-02-09
@Umr001

LI.add(1231);

In Java, not functions but class methods, you are trying to call a method on a class field outside of the method, in the class definition. Yes, it's not possible with anything at all. If you need to set the initial value of a component, then do it in the constructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question