Which two code fragments correctly create and initialize a static array of int elements? (Choose
two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{ 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Answer: A,B

Can any one explain how??? Thank you

The first is standard shortcut syntax for the creation and initialization of an array in the same statement it is declared.

The second is using a static code block to create and initialize.

I use the above syntax in eclipse , but it given me the error..... Any suggestion

could you post what the error says?

1) Illegal modifier for parameter a; only final is permitted
2) Cannot define dimension expressions when an array initializer is provided

Were you defining those at the class level or in a method? Static declarations like that only apply at the class level.

I'm guessing you got error 2 with statement C? C is not valid syntax.

Well its just a question... I saw it in a book and answer's are these.... I just want to know the pofessional suggestions

Well its just a question... I saw it in a book and answer's are these.... I just want to know the pofessional suggestions

have you checked the book itself? normally somewhere in the book there's a list of the correct answers that come with explanations of why the answers are (in)correct.
mostly these questions are right behind the chapter where they explain which techniques are correct and what the (dis)advantages are.

> Well its just a question
And I just attempted to explain. You didn't bother to answer my questions about the errors you received, so I can't really clarify why you received them.

commented: Want your autograph +8

Sorry... I have only used A and B statement.. I asked my teacher today, he said both are right answer's but its an assignement to figure it out how? I applied them in main , also in constructor... Now after using them at class level i finally found my answer.... Greate Job.... I just want a autograph from u. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.