In Object Array = all elements are same… After assign data

后端 未结 3 1461
太阳男子
太阳男子 2021-01-21 23:47

For Example if I Create array of object And assing data...

short version of problem. array[0].init(\"ce\", 2) array[1].init(\"nh\", 2)

Output... Of array[0] Will

相关标签:
3条回答
  • 2021-01-22 00:00

    If a data member is static, this means that it is shared by all instances of the class:

    public static String ch_v;
    public static int x = 0, y = -5, y_max = 325;
    

    Remove the two static modifiers.

    0 讨论(0)
  • 2021-01-22 00:08

    In class ulamek:

    Change:

    public static String ch_v;
    public static int x = 0, y = -5, y_max = 325;
    

    to:

    public String ch_v;
    public int x = 0, y = -5, y_max = 325;
    

    Declaring a variable or method static means that its value is available across all classes.

    0 讨论(0)
  • 2021-01-22 00:09

    The fields in your ulamek class are static's

    It means that they belong to the ulamek Type, and not it's instances (objects).

    Alter it this way:

    class ulamek
    {
        public String ch_v;
        public int x = 0, y = -5, y_max = 325;
    ...
    

    And it should work.

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