Results 1 to 2 of 2
- 08-08-2007, 05:32 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 8
- Rep Power
- 0
passing an array into an instance
Hi. I am just wandering how do I pass an array from one class into another class via a constructor?
In the class that i collect my information in, I have the following code:
the name of the array is campuses.Java Code:units[Unit.unitCount] = new Unit(name,code,sem1,sem2,campusCount,campuses);
in my Unit class, my Constructor looks like this...
but when i try to do a toString to print off values of the array, every value comes up as null, so I can only assume that they are not being set correctly in the first place.Java Code:public Unit(String iName, String iCode, char iSemester1, char iSemester2, int iCampusCount) { name=iName; code=iCode; campuses= new String[10]; semester1=iSemester1; semester2=iSemester2; unitCount++; campusCount=iCampusCount;
Any help would greatly be appreciated.
- 08-08-2007, 09:35 AM #2
These are different constructors. The top one has six arguments. The bottom on has five arguments. The top statement will not call the bottom constructor.Java Code:new Unit(name, code, sem1, sem2, campusCount, campuses); Unit(String iName, String iCode, char iSemester1, char iSemester2, int iCampusCount) { name=iName; code=iCode; campuses= new String[10]; semester1=iSemester1; semester2=iSemester2; unitCount++; campusCount=iCampusCount;
In the bottom one, since campuses is instantiated
but not initialized by assignment of constructor local variable/argumentsJava Code:campuses= new String[10];
or by initialization of each elementJava Code:this.campuses = campuses;
I would expect all elements to be null.Java Code:for(int j = 0; j < campuses.length; j++) campuses[j] = "";
Similar Threads
-
Instantiation using an instance factory method
By Java Tip in forum Java TipReplies: 0Last Post: 03-29-2008, 12:35 PM -
Create different instance of a tablemodel
By Bill in forum AWT / SwingReplies: 6Last Post: 03-27-2008, 03:49 PM -
New Instance for SWT
By srinivasa_v in forum SWT / JFaceReplies: 1Last Post: 08-08-2007, 01:02 AM -
problems with Instance Data
By paty in forum New To JavaReplies: 2Last Post: 08-02-2007, 05:45 PM -
Instance variable
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks