This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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;
...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.
Joined: Jul 28, 2004
Miguels answer helped me fine but I thought that Stan asked a question and I would have a go at answering it. I think in the end I've learnt more by trying to answer Stan's question. Many thanks
Joined: Jan 29, 2003
I was after a different problem description perhaps. I guessed the assignment is: "Create an object that contains an array. We'll tell the object how big the array should be through the constructor." So my missing line would be something that creates a new array of the specified size.
How do you make a new array? How do you tell it how big to be?
Your answer might be: That wasn't the assignment at all! Then I'll just crawl away.
Be careful: Programmers new to the Java programming language are tempted to follow length with an empty set of parenthesis. This doesn't work because length is not a method. length is a property provided by the Java platform for all arrays.