Understanding Java's "final" for translation to C#
c#, final, java
Solution
I think you have a misunderstanding of the `final` keyword semantic when it is applied to arrays in Java.
In both Java examples the arrays will remain unchanged, but their elements may be changed. All your assignments will be executed correctly, and the values stored in the array will get changed. However, if you try
final int[] PRED = new int[this.Nf];
// some other code
PRED = new int[123]; // <<== Compile error
you are going to see a compile error.
When translating your code to C#, you may need to translate `final` either as `sealed` (when it is applied to a `class`), or as `readonly` (when it is applied to a member). The semantic of `readonly` arrays in C# and `final` arrays in Java are the same: your program cannot reassign the array, but it can freely modify its elements.
Finally, there is a Java-specific case when `final` is used where you wouldn't need it in C# at all: when you need to use a variable inside a method of an anonymous local class in Java, you must make that variable `final`. Since C# does not have anonymous local classes, you would need to translate that piece of code with something else, perhaps with anonymous delegates. Such delegates are not restricted to using readonly variables.
Problem
I am not a Java programmer. I read the documentation on "final", and understand it to mean "a variable's value may be set once and only once." I am translating some Java to C#. The code does not execute as expected. I tried to figure out why, and found some uses of final that don't make sense. Code snippet 1: ``` final int[] PRED = { 0, 0, 0 }; ... PRED[1] = 3; ``` Will PRED[1] be 0 or 3? Code snippet 2: ``` final int[] PRED = new int[this.Nf]; for (int nComponent = 0; nComponent < this.Nf; nComponent++) { PRED[nComponent] = 0; } ... PRED[1] = 3; ``` Surely PRED[0] will remain as 0?