Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-08-2007, 06:32 AM
Member
 
Join Date: Aug 2007
Posts: 8
lockmac is on a distinguished road
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:

Code:
units[Unit.unitCount] = new Unit(name,code,sem1,sem2,campusCount,campuses);
the name of the array is campuses.

in my Unit class, my Constructor looks like this...

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;
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.

Any help would greatly be appreciated.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-08-2007, 10:35 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,145
hardwired is on a distinguished road
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;
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.
In the bottom one, since campuses is instantiated
Code:
campuses= new String[10];
but not initialized by assignment of constructor local variable/arguments
Code:
this.campuses = campuses;
or by initialization of each element
Code:
for(int j = 0; j < campuses.length; j++) campuses[j] = "";
I would expect all elements to be null.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Instantiation using an instance factory method Java Tip Java Tips 0 03-29-2008 01:35 PM
Create different instance of a tablemodel Bill AWT / Swing 6 03-27-2008 04:49 PM
New Instance for SWT srinivasa_v SWT / JFace 1 08-08-2007 02:02 AM
problems with Instance Data paty New To Java 2 08-02-2007 06:45 PM
Instance variable Jack New To Java 2 07-04-2007 05:00 AM


All times are GMT +3. The time now is 10:17 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org