How to use static ArrayList in Java

1 Answer

0 votes
import java.util.*; 
   
public class MyClass {
    static ArrayList<Integer> al = new ArrayList<>();

    static void addItems() {
        al.add(10);
        al.add(20);
    }
    
    public static void main(String args[]) {
        al.add(4);
        al.add(7);
        al.add(8);

        addItems();

        for (Integer n : al) {
            System.out.println(n);
        }
    }
}



/*
run:

4
7
8
10
20

*/

 



answered Sep 6, 2020 by avibootz

Related questions

1 answer 112 views
112 views asked Feb 3, 2024 by avibootz
1 answer 126 views
1 answer 124 views
1 answer 202 views
2 answers 236 views
1 answer 128 views
128 views asked Apr 5, 2019 by avibootz
...