File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Object arrays in constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object arrays in constructors" Watch "Object arrays in constructors" New topic

Object arrays in constructors

Sal Velinus

Joined: Mar 05, 2002
Posts: 25
Below is code for a class Computer. MemoryChip is another class, it works fine. I'm having trouble trying to figure out how to work with the arrays of MemoryChip in the constructors and with method addAChip(). I don't like writing duplicate code, but maybe I'll have to in spots. Will the constructor have to call addAChip() twice, and addAChip() have to call setRam()? That doesn't seem right.
Thanks for any help.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Greg,
It is not clear what you are trying to do with the MemoryChip. If you are just trying to give the computer some amount of memory then I would probably just pass in an integer for the amount of chips instead of passing in the actually MemoryChip. Something like the following:

If your addAChip method is supposed to give some default value to the new chip then you can go ahead and call it inside the loop after the initialization line that already exists.
Sal Velinus

Joined: Mar 05, 2002
Posts: 25
Well, that would no doubt be a good solution, but I have to code according to a UML diagram I was given that includes all the methods, variables and parameters. I can add to it, or change to more advanced stuff like lists, but can't take away. The trouble is with the constructor with MemoryChip parameters, and the setRam() and addAChip methods(). To quote the UML, addAChip() is a:
Convenience method that takes the chip object that was passes in and adds it to the array at the index specified as the 2nd parameter.
Some of these problems may be my design thinking. I think all constructors here should chain to the final constructor with this calls, so all the work is done there. ram should only be set in setRam(). But where does the parameter MemoryChip[] chips come from in the constructor to pass to setRam()? It can't be ram, because that's what I'm setting. Do I have to create a new MemoryChip[]? Does setRam() call addAChip()? This is where I'm getting stuck.
Thanks for all help, I appreciate it.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why not ask the person who gave you the UML diagram what he/she had in mind.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Sal Velinus

Joined: Mar 05, 2002
Posts: 25
She's on vacation for two weeks.
Sal Velinus

Joined: Mar 05, 2002
Posts: 25
Ok, this is what I came up with, and it works, but seems a sloppy way to do it. It seems odd to declare another array in the constructor, and to call addAChip twice. Any ideas on a better way? setRam() seems redundant as well. Thanks.

[ April 02, 2002: Message edited by: Greg Adams ]
I agree. Here's the link:
subject: Object arrays in constructors
Similar Threads
Keep getting ioexception...please help
No getter method for property: "AcCode" of bean: in "com.myapp.struts.fmdBEForm"
Problems with vectors
Super or this?
Removing multiple if conditions