[Solved] Adding objects to ArrayList [duplicate]


If I understand the problem correctly, you have 6 Publication objects, and you are only seeing the values of the most recently created one.

That would likely be caused because you have static class variables instead of instance variables.

For example

class A {
    static int x; // class variable
    int y;        // instance variable

    public A(int val) {
        x = val; // All 'A' classes now have x = val;
        y = val; // Only 'this' class has y = val;
    }
}

If I were to run this

A a1 = new A(4);
A a2 = new A(5);
System.out.println(a1.x);

Then I would see it print 5 and not 4, which describes the scenario you are seeing because you have assigned all variables in the Publication class to those that you use during the last call of new Periodical.

The solution is to not use static variables if you want to have multiple instances of a class with their own values.

2

solved Adding objects to ArrayList [duplicate]