Creating an array in constructor ... defaults all values?
For some reason I am having a very difficult time trying to initialize and store values in arrays within a constructor. When I use the debugger to check the arrays values, the Boolean all default to false and my other double array all default to 0s.
Pretend I have this:
Code :
public class shank
{
double h;
double m;
boolean t1;
boolean t2;
boolean t3;
boolean t4;
int peeps;
double [] huy = new double [3];
boolean [] rup = new boolean [3];
shank(int popes, boolean r1; boolean r2; boolean r3; boolean r4) // two of these are true and 2 are false (boolean)
{
peeps = popes;
t1 = r1;
t2 = r2;
t3 = r3;
t4 = r4;
double [] huy = {586.1, 429.3, 392.3, 3492.3}; //this defaults to all 0s.
boolean [] rup = {t1, t2, t3, t4}; //this defaults to all false.
}
}
I figured the arrays default because I declare them twice, but if I don't, then I just get an error...Can someone direct me the correct way of getting my arrays correctly initialized. I should mention that I have two classes, one is the tester and this is the class that is called with the methods. I prefer that no more parameters be added to the constructor.
Re: Creating an array in constructor ... defaults all values?
You need to define the variables at the class level and assign them values in the constructor.
You are defining two different variables with the same names. When the constructor exits, the local variables: huy and rup go away.
You can probably move the definitions from the constructor to the class level and replace the class level definitions with the moved ones.
Re: Creating an array in constructor ... defaults all values?
Ah, I did something like that, and got it working. Before I realized this forum was back to normal, I had the guys at Stack Overflow forum help me out. Thanks though.