File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
i've got to create a constructor that accepts the size of an array. I've come up with this

Where sa is the name of the array, it has been consructed in another class called student
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281


hope this helps


java amateur
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See if this is where you're going ...

Can you fill in the missing line in the constructor?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
In answer to Stans question would it be?
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
also thanksnow Miguel it works fine now
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Originally posted by Iain Palmer:
In answer to Stans question would it be?


No, not quite. myArray hasn't been defined yet, so you'll actually get a compiler error. Try again, keeping in mind what you want to do -- define myArray as an integer array of size size.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
how about
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
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
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
from sun java tutorial:
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.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
and yet, for Strings, "length" is a method instead of a property. this has tripped me up on more than one occasion!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor