A simple question: is it possible to initialize an array in an interface?
Yes.
This works, but I want to initialize the array on a for basis. Ok thanks for the help
This is not an easy question;)
You cannot do this strictly because you cannot add a static block to the interface. But you can have nested classor enum.
IMHO, this may be more confusing than useful:
public interface I {
int[] values = Init.getValue();
enum Init {;
static int[] getValue() {
int[] arr = new int[5];
for(int i=0;i<arr.length;i++)
arr[i] = i * i;
return arr;
}
}
}
source
share