java - Array initialization syntax when not in a declaration -


i can write:

aclass[] array = {object1, object2} 

i can write:

aclass[] array = new aclass[2]; ... array[0] = object1; array[1] = object2; 

but can't write:

aclass[] array; ... array = {object1, object2}; 

why blocked java?

i know how work around it, time time simpler.

for example:

public void selectedpointstomove(cpvect coord) {      if (tab == null) {         if (arepointsclose(coord, point1, 10)) {             cpvect[] temptab = {point1};             tab = temptab;         } else if (arepointsclose(point2, coord, 10)) {             cpvect[] temptab = {point2};             tab = temptab;         } else {             cpvect[] temptab = {point1,point2};             tab = temptab;         }     } } 

this simple question has been bugging me since learned how play arrays in java.

why blocked java?

you'd have ask java designers. there might subtle grammatical reason restriction. note of array creation / initialization constructs not in java 1.0, , (iirc) added in java 1.1.

but "why" immaterial ... restriction there, , have live it.

i know how work around it, time time simpler.

you can write this:

aclass[] array; ... array = new aclass[]{object1, object2}; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -