0

Ok... i'm working with some Matrix. Specifically inverting them with the Gauss algorithm. the inversion succeded, but when i restart the activity that takes the input matrix, the result, which is enlistened in an Arraylist gets overwrited by the previous operations.

switch(n){
        case 2: identityMatrix = Matrix.ID_MATRIX_OR2;
            break;
        case 3: identityMatrix = Matrix.ID_MATRIX_OR3;
            break;
        case 4: identityMatrix = Matrix.ID_MATRIX_OR4;
    }

when it execute this code the 2nd time i request an inversion, instead of the identity matrix it copies the value of the PREVIOUS inverted matrix, which is TEMPORARILY stored in "identityMatrix" , on the new inversion "identityMatrix" became null again

Checking in debug the "Matrix.ID_MATRIX_OR"number where modified.

THAT SHOUDLN'T BE as they are declared as -- PROTECTED FINAL STATIC --

protected final static Fraction[][] ID_MATRIX_OR2 = {   {ONE_M,ZERO_M},
                                                        {ZERO_M,ONE_M}};
protected final static Fraction[][] ID_MATRIX_OR3 = {   {ONE_M,ZERO_M,ZERO_M},
                                                        {ZERO_M,ONE_M,ZERO_M},
                                                        {ZERO_M,ZERO_M,ONE_M}};
protected final static Fraction[][] ID_MATRIX_OR4 = {   {ONE_M,ZERO_M,ZERO_M,ZERO_M},
                                                        {ZERO_M,ONE_M,ZERO_M,ZERO_M},
                                                        {ZERO_M,ZERO_M,ONE_M,ZERO_M},
                                                        {ZERO_M,ZERO_M,ZERO_M,ONE_M}};

where ONE_M and ZERO_M are custom object Fraction, that values are 1 and 0 and they also are PROTECTED STATIC FINAL.

So why they get modified? shouldn't the final modifier prevent this?

or should I search for something like the flush() method to start new?

  • yeah -.-' forgot to add the tag for java, more specifically, Java in android studio – Deryl_Myrawill Jun 30 '23 at 16:02
  • 2
    better post a [mre] - not clear what you are asking, which value has changed? || BTW a final variable only means the variable itself cannot be changed, that is, the value it contains cannot be changed, no new instance/array can be assigned to it; but the contents of that instance/array can be changed (in your example, `ID_MATRIX_OR2 = ANOTHER_MATRIX` will not be allowed, but `ID_MATRIX_OR2[0][0] = ZERO_M` is allowed - the array is NOT immutable) – user16320675 Jun 30 '23 at 16:14
  • 1
    final just means a variable cannot be reassigned. It doesn't make object immutable. For that you have to use an immutable data structure, which arrays are not. – OH GOD SPIDERS Jun 30 '23 at 16:22
  • possibly related: [Immutable array in Java](https://stackoverflow.com/questions/3700971/immutable-array-in-java) – OH GOD SPIDERS Jun 30 '23 at 16:24
  • RESOLVED... i reconstructed an id Matrix based on the N value, which is the dimension @user16320675 it was the matrix declared as FINAL that ws modified. Now no more. thanks everybody – Deryl_Myrawill Jun 30 '23 at 16:53

0 Answers0