Home > Blockchain >  Weird issue when declaring ArrayList as static in Java
Weird issue when declaring ArrayList as static in Java

Time:05-22

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.

CodePudding user response:

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.

CodePudding user response:

You are creating a new instance of NewClass instead of adding to the ArrayList of the same NewClass.

In order to add objects to the static ArrayList you'll need to create only one instance of NewClass and create a static method inside this class to handle adding objects to the static ArrayList.


public static void main(String[] args) {

    NewClass class = new NewClass("Toyota", "Camry", "gray", "120,000");
    class.addToList("Hyundai", "Sonata", "red", "100,000");
    class.addToList("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 = "";

    public NewClass(String make, String model, String color, String price){
        addToList(make, model, color, price);
    }

    public static void addToList(String make, String model, String color, String price) {
        carsInfo.add("Make: "   make   " \t ");
        carsInfo.add("Model: "   model   " \t ");
        carsInfo.add("Color: "   color   " \t ");
        carsInfo.add("price:  "   price   " SAR\n");

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

        carsInfo.clear()
        PrintAll  = cars.get(0);
    }

}

  • Related