Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize array in an interface?

Is it possible to initialize an array in an interface using a for instruction?

like image 860
user1555754 Avatar asked Aug 14 '12 08:08

user1555754


1 Answers

Simple question - Is it posible to initalize array in an interface?

Yes.

This works but i want to initialize array by "for" intsruction. Ok thanks for help

That's not a simple question ;)

You can't do this strictly because you can't add a static block to an interface. But you can have a nested class or enum.

IMHO, that could be more confusing than useful as follows:

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;
        }
    }
}
like image 81
Peter Lawrey Avatar answered Oct 04 '22 22:10

Peter Lawrey