0

i've two class. the first class is

public class A{

    private static int[] x = {0,0,0,0};
    private static int justX = 0;

    public A(){}

    public static int[] getX(){return x;}
    public static int getJustX(){ return justX;}
}

and the second class is

public class B extends . . .{
    private int[] x;
    private justX;

    public B(){
        x = A.getX(); 
        justX = A.getJustX();
        Log.d("X: ", Arrays.toString(x)); //X: {0,0,0,0}
        Log.d("JUST X: ", String.value(justX)); //JUST X: 0;
    }

    private void onTouch(){
        x[0] = 1;
        x[1] = 1;
        x[2] = 1;
        x[3] = 1;
        justX = 1;

        Log.d("X: ",Arrays.toString(A.getX())); //X: {1,1,1,1}
        Log.d("JUST X: ", String.value(A.getJustX())); //JUST X: 0;
    }
}

array variable from class A changed but integer variable has not changed, Why did it happen?

1
  • 1
    Java objects use value pointer to keep reference with it's objects. So A.x and B.x points to the same value. Commented Feb 24, 2016 at 18:54

2 Answers 2

2

What you're encountering is that Java is always pass-by-value for primitive types (like int), but for object types, it's a little more complicated (and arrays are objects). Specifically, what happens is that assigning to a variable with a reference type changes what object it refers to, but doesn't create a new object. Assigning to a primitive variable just changes the value of that variable.

Let's walk through things.

public class B extends . . .{
    private int[] x;
    private justX;

    public B(){
        x = A.getX(); 

At this point, you've made B.x into a reference to the same array that A.x is a reference to. So they're both names for the same data structure.

        justX = A.getJustX();

And here justX just got the value that A.justX had (0).

        Log.d("X: ", Arrays.toString(x)); //X: {0,0,0,0}
        Log.d("JUST X: ", String.value(justX)); //JUST X: 0;
    }

    private void onTouch(){
        x[0] = 1;
        x[1] = 1;
        x[2] = 1;
        x[3] = 1;

Here, you've reached inside B.x, and changed the values of parts inside it. But it's the same object that A.x also refers to, so when you look at A.x, you'll see those same changes.

        justX = 1;

And here you're just taking B.justX and giving it a new value.

        Log.d("X: ",Arrays.toString(A.getX())); //X: {1,1,1,1}
        Log.d("JUST X: ", String.value(A.getJustX())); //JUST X: 0;
    }
}

Hopefully that explains why you're getting the result you're getting. If you want to change A.justX, you'll need to do so explicitly. If you want to be able to change B.x without also changing A.x, then you'll need to explicitly make a new object (for instance, with .clone()).

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, its solved my question. I just found out about it, coz i learned java not from the beginning. before i know about clone() i just set B.x using looping from int[] tmp = A.getX() and set B.x. Thank Adam.
0

The problem is that you are changing the copy of the array. Once you are done changing the array and justX, you have to set it again.

public class A{

    private static int[] x = {0,0,0,0};
    private static int justX = 0;

    public A(){}

    public static int[] getX(){return x;}
    public static int getJustX(){ return justX;}
    public static void setX(int[] a){x = a;};
    public static void setJustX(int j){justX = j;}
}

and

public class B extends . . .{
    private int[] x = A.getX();
    private justX = A.get;

    public B(){
        x = A.getX(); 
        justX = A.getJustX();
        Log.d("X: ", Arrays.toString(x)); //X: {0,0,0,0}
        Log.d("JUST X: ", String.value(justX)); //JUST X: 0;
    }

    private void onTouch(){
        x[0] = 1;
        x[1] = 1;
        x[2] = 1;
        x[3] = 1;
        justX = 1;
        A.setX(x);
        A.setJustX(justX);

        Log.d("X: ",Arrays.toString(A.getX())); //X: {1,1,1,1}
        Log.d("JUST X: ", String.value(A.getJustX())); //JUST X: 0;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.