简体   繁体   English

在 Java 中将 ArrayList 声明为静态时的奇怪问题

[英]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 :只是为了让您理解我的问题,这是我将两个 ArrayLists 声明为static时的输出:

Make: Toyota Model: Camry Color: gray price: 120,000 SAR品牌:丰田 车型:凯美瑞 颜色:灰色 价格:120,000 沙特里亚尔
Make: Toyota Model: Camry Color: gray price: 120,000 SAR品牌:丰田 车型:凯美瑞 颜色:灰色 价格:120,000 沙特里亚尔
Make: Toyota Model: Camry Color: gray price: 120,000 SAR品牌:丰田 车型:凯美瑞 颜色:灰色 价格:120,000 沙特里亚尔

when it should be :什么时候应该:

Make: Toyota Model: Camry Color: gray price: 120,000 SAR品牌:丰田 车型:凯美瑞 颜色:灰色 价格:120,000 沙特里亚尔
Make: Hyundai Model: Sonata Color: red price: 100,000 SAR品牌:现代 型号:索纳塔 颜色:红色 价格:100,000 沙特里亚尔
Make: Hyundai Model: Accent Color: blue price: 60,000 SAR品牌:现代 型号:口音 颜色:蓝色 价格:60,000 沙特里亚尔

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.这就是为什么当您创建此类的新实例时,您总是会在 ArrayLists 中填充那些第一个值 - 构造函数不断推送到这些静态变量,并且由于它们不属于对象的任何实例,这些值只是不断添加,这也是它在没有 static 关键字的情况下工作的原因。

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).我建议将这些 ArrayList 字段设为私有且非静态(除了 PrintAll 字段)。 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.然后,您可以为那些非静态字段创建公共访问器,这样其他类可以实例化 NewClass(),调用其上的访问器,它们可以返回该字段的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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