0

Is there a way to initialize multiple objects at once in a similiar way:

SimpleDoubleProperty sx, sy, vx, vy, dt, ro, w, angle, steps;
SimpleDoubleProperty[] prop = {sx,sy,vx,vy,dt,ro,w,angle,steps};

double[] initial = {0,0,10,10,0.1,1,1,60,20};

for(int i=0; i<prop.length; i++){
    prop[i] = new SimpleDoubleProperty(initial[i]);
}    

Main goal is to be able to use sx, sy.. variables on their own and shorten the code. I know that i'm overriding those variables (they are not stored in this array anymore) at this point:

prop[i] = new SimpleDoubleProperty(initial[i]);

but that was only an example to better present this problem.

double[] initial = {0,0,10,10,0.1,1,1,60,20};
sx = new SimpleDoubleProperty(initial[0]);
sy = new SimpleDoubleProperty(initial[1]);
vx = new SimpleDoubleProperty(initial[2]);
vy = new SimpleDoubleProperty(initial[3]);
dt = new SimpleDoubleProperty(initial[4]);
ro = new SimpleDoubleProperty(initial[5]);
w = new SimpleDoubleProperty(initial[6]);
angle = new SimpleDoubleProperty(initial[7]);
steps = new SimpleDoubleProperty(initial[8]);  

Is there another way?

  • are you concerned at shortening the code lines ? – Karthik Mar 11 '17 at 08:28
  • Well only a bit but the main reason i ask is curiosity. – Janusz Waligóra Mar 11 '17 at 08:32
  • The only thing you could do to shorten this is creating a method for creating the properties which could be called like this: `sx = m(initial, 0); sy = m(initial, 1); ...` (Everything else would simply make the code longer unless you're sure reflection can identify the order of the fields which would make the code hard to maintain.) – fabian Mar 11 '17 at 08:32
  • So there is another way when i'm sure about the order? – Janusz Waligóra Mar 11 '17 at 08:38
  • 1
    Why would you want to shorten the code? What are the benefits and costs? – Lew Bloch Mar 11 '17 at 12:42
  • Or you could use an anonymous initialisation block: http://stackoverflow.com/questions/3987428/what-is-an-initialization-block – Matthias Danetzky Mar 11 '17 at 14:03

0 Answers0