integer array static initialization
The correct answers are 1 and 2 (or A and B with your notation), and an also correct solution would be:
static final int[] a = new int[]{ 100,200 };
Solution D doesn't initalize the array automatically, as the class gets loaded by the runtime. It just defines a static method (init), which you have to call before using the array field.
D defines a static method for initialising a
but does not actually call it. Thus, a
remains uninitialised unless someone explicitly calls the init
method.
As other answers have pointed out: D shouldn't even compile because it attempts to assign a value to the final
variable a
. I guess that's a much more correct explanation. Nevertheless, even if a
was not final D would still not work without extra code.
I assume the new int[3]
in D is a typo? The other three all attempt to create an array of length 2.