posted 19 years ago
There seems to be some confusion over what exactly you're trying to do.
Do you already have an array created, and you just want to find out the length of that already-existing array? And then you want to pass that length to the constructor of another class?
If so, then Migeul's code is right on the money. He's creating an array (in this case, it has 17 elements), and he's asking that array how long it is, using the length() method. That method will return 17 of course, and that result of 17 is therefore what gets passed to the constructor. The constructor then simply stores that value in an instance variable in your new object.
Okay, so on to the second possiblity:
Do you not already have an array created? And do you want to pass an arbitrary number (could be 17, could be 5, could be anything) to the constructor of a class? And do you then want that construction to create a new array right there on the spot, using the number you gave it (17, or 5, or whatever) to determine the size of this newly-created array?
If so, then Stan's question is leading you in the right direction. He's set it up so that you can pass any number to a constructor. It could be anything--that all depends on how you call the constructor. It's intended to be used like this:
Demo myDemo1 = new Demo(17);
Demo myDemo2 = new Demo(5);
See, when we use "new" to create a new instance of Stan's Demo class, we pass a number to the constructor. The question he's asking is, how would you get your construction to create a brand-new array of the size we've asked for?
Remember, the constructor doesn't know how big it will be asked to make the array, so you can't just type in a number like this:
myArray = new int[17];
...because you don't know that 17 is the correct size. But you do know that, whatever the size should be, that size will be passed as an argument and stored in that integer variable called "size".
Hopefully that helps.
- Jeff