3

Suppose I have a class

    public class c1 {
        public static ArrayList<String> list = new ArrayList<String>();

        public c1() {
            for (int i = 0; i < 5; i++) {   //The size of the ArrayList is now 5
                list.add("a");
            }
        }
    }

But if I access the same ArrayList in another class, I will get a list with SIZE = 0.

     public class c2 {
         public c2() {
             System.out.println("c1.list.size() = " + c1.list.size()); //Prints 0
         }
     }

Why is this happening. If the variable is static, then why is a new list being generated for class c2? How can I make sure that I get the same ArrayList if I access it in a different class?

/****Revised code********/

     public class c1 {
        public static ArrayList<String> list = new ArrayList<String>();

        public static void AddToList(String str) {       //This method is called to populate the list 
           list.add(str);
        }
    }

But if I access the same ArrayList in another class, I will get a list with SIZE = 0, irrespective of how many times I called AddToList method.

     public class c2 {
         public c2() {
             System.out.println("c1.list.size() = " + c1.list.size()); //Prints 0
         }
     }

How can I make sure that same changes appear when I use the ArrayList in another class?

Vini
  • 313
  • 1
  • 7
  • 21

1 Answers1

7

In your code as is, you should call the c1 constructor in order to fill the ArrayList. So you would need:

public c2() {
    new c1();
    System.out.println("c1.list.size() = " + c1.list.size()); //Prints 0
}

But this is not good. The best approach would be static initialization by using a static block code in c1 class:

public class c1 {
    public static ArrayList<String> list = new ArrayList<String>();

    static {
        for (int i = 0; i < 5; i++) {   //The size of the ArrayList is now 5
            list.add("a");
        }
    }

    public c1() {

    }
}

As a recommendation from What does it mean to "program to an interface"?, it would be better to declare the variable as List<String> and create the instance as ArrayList:

public static List<String> list = new ArrayList<String>();

Another recommendation, use a static method to access to this variable instead of making it public:

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

public static List<String> getList() {
    return list;
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks for the help. It worked. But if I want to fill my ArrayList in class c1 through a method, then how can I make sure that same changes appear when I use the ArrayList in another class? – Vini Mar 31 '13 at 15:40
  • @VijendraSinghAswal you can do it in a `static` method as well, and you must be sure that this `static` method should be called before using the `list`. – Luiggi Mendoza Mar 31 '13 at 16:05
  • could you please give an example of calling this in a static method.I tried but it works only if I put in a static block. – sathishvisa Dec 09 '13 at 22:06
  • @sathishvisa I don't understand your request, which problems do you have? – Luiggi Mendoza Dec 09 '13 at 22:08
  • @sathishvisa if you're declaring your `List` as `static`, then you should initialize it in a static block. – Luiggi Mendoza Dec 09 '13 at 22:11
  • I have a static method that populates this `public static List` dynamically.I am trying to access that in another class, but I always get empty array. – sathishvisa Dec 09 '13 at 22:14
  • @sathishvisa only after reading [your question](http://stackoverflow.com/q/20482603/1065197) I really understood your problem... – Luiggi Mendoza Dec 09 '13 at 23:05