简体   繁体   中英

Weird issue when declaring ArrayList as static in Java

public static void main(String[] args) {

    NewClass Camry = new NewClass("Toyota", "Camry", "gray", "120,000");
    NewClass Sonata = new NewClass("Hyundai", "Sonata", "red", "100,000");
    NewClass Accent = new NewClass("Hyundai", "Accent", "blue", "60,000");

    System.out.println(NewClass.PrintAll);   

}

...

public class NewClass {

    static ArrayList<String> cars = new ArrayList<>();
    static ArrayList<String> carsInfo = new ArrayList<>();

    static String PrintAll = "";

    String make = "";
    String model = "";
    String color = "";
    String price = "";

    public NewClass(String make, String model, String color, String price) {

        this.make = make;
        this.model = model;
        this.color = color;
        this.price = price;

        carsInfo.add("Make: " + this.make + " \t ");
        carsInfo.add("Model: " + this.model + " \t ");
        carsInfo.add("Color: " + this.color + " \t ");
        carsInfo.add("price:  " + this.price + " SAR\n");

        cars.add((carsInfo.get(0) + carsInfo.get(1) + carsInfo.get(2) + carsInfo.get(3)));

        PrintAll += cars.get(0);
    }

}

Just so you can understand my problem, this is the output when I declare the two ArrayLists as static :

Make: Toyota Model: Camry Color: gray price: 120,000 SAR
Make: Toyota Model: Camry Color: gray price: 120,000 SAR
Make: Toyota Model: Camry Color: gray price: 120,000 SAR

when it should be :

Make: Toyota Model: Camry Color: gray price: 120,000 SAR
Make: Hyundai Model: Sonata Color: red price: 100,000 SAR
Make: Hyundai Model: Accent Color: blue price: 60,000 SAR

For some reason, the first object values get repeated with every other object.
without using static everything is fine, but I need the Arrays to be static so I can access them from another class. So I really want to know what is causing this.

The static fields are usually also called “variables of the class”. Their value can always be initialized even if the class is not instantiated. This is why, when you create a new instance of this class, you always get those first values filled in the ArrayLists - the constructor keeps pushing to those static variables and since they don't belong to any instance of an object, the values just keep getting added and also this is why it works without the static keyword.

Generally, the usage of static is mostly not a good idea. Right now, I can only think of it being useful for constants - comment down if I'm missing something. I suggest that those ArrayList fields are made private and non-static (in addition to the PrintAll field). Then, you can create public accessors for those fields that are non-static and this way other classes can instantiate NewClass(), call the accessors on it and they can return the value of the field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM