Java sharing same values in a class

后端 未结 4 500
不知归路
不知归路 2021-01-29 13:03

I have a class called Hand and another class to test it. Hand uses global variables and change their values with some methods if in class test I create

相关标签:
4条回答
  • 2021-01-29 13:38

    Make private List<Card> hand = new ArrayList<Card>(); as instance variable rather then make it static..
    Coz static is property of class and instance variables are property of Object... which is different for different Object ..but in case of static they are same for all objects..

    0 讨论(0)
  • 2021-01-29 13:41

    Turn the private static List<Card> into a private List<Card>.

    0 讨论(0)
  • 2021-01-29 13:43

    The static keyword makes the variable class-scoped, not instance-scoped. That means the same value is common for all instances of the class. A non-static member is particular to every instance of the class. Remove the static keyword and you will get the desired behavior.

    0 讨论(0)
  • 2021-01-29 13:51

    Remove the static modifier which is common for all instance of the class loaded in a particular class loader.

    0 讨论(0)
提交回复
热议问题