0

When filling in the values for a class-wide (static) data structure/collection (in this case a 2D array), is it generally better to save the values directly into the static one, or should a temporary variable be created in the method?

To make it more clear:

public class MyClass{

    private static int[][] table;

    public void setTable(int row, int col){

        table = new char[row][col];
        // fill table (eg. table[i][j] = 5;)
    }


    //// OR THIS//////

    public void setTable(int row, int col){

        int[][] tempTable = new tempTable[row][col];

        // fill tempTable
        table = tempTable;
    }
}

Creating a temporary variable feels redundant and useless here. But are there cases where this would be advised? Possibility of program crashing halfway through execution of method and corrupting data? Multi-threaded applications?

I hope the question isn't subjective, just want to know if there is inherently something wrong with just filling it in directly.

1
  • If the static object can/should be changed, then the first way is fine. I prefer it becuase of clarity, but that's due to my preference, not any technical advantage. But if you want it to remain unmodifiable, then you should make it static final int[][] table = setTable(int, int), and use the second approach. Commented Oct 4, 2014 at 2:57

1 Answer 1

1

First, it is important to notice that neither of the two approaches is thread-safe. If your application is multi-threaded or, more precisely, MyClass objects are accessed concurrently, you'll need extra precautions. Even assignments of reference types are not guaranteed to be atomic.

In a sequential program, building the new value in a temporary and only overwriting the old reference once this is done can be safer in some aspects. Most important, consider what would happen if the logic that populates the new table throws an exception that escapes from the setTable method. If your program manages to recover from that error, the first implementation will have left behind an undefined state of MyClass.table while the second will have left it unchanged. So if it is possible that the process fails with an error you plan to recover from, you probably need to go with the second implementation. (In C++, we say an operation that provides this kind of “commit-or-rollback” semantics provides a strong exception guarantee but I have not seen this term used in the Java community so far.)

Finally, you might be interested to hear that

Object obj = new Object();

does not guarantee that the assignment only takes place after the new object is fully constructed. If this might be important for you, move the construction into a method since returning from a method (as opposed to a constructor) establishes a “happens before” relationship. (The assignment will still not be atomic, as mentioned above.)

I assume that you are aware of the implications that using a mutable static filed have. Just saying that there is likely a better way to do what you want.

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

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.