Comp Sci Java:Constructors within constructors

  • Thread starter Thread starter Live4eva_2
  • Start date Start date
AI Thread Summary
The discussion revolves around creating an array of AddressBook objects in Java, with a focus on using constructors within constructors. The user is attempting to instantiate Person, Date, and Address objects within the Address constructor but struggles with referencing the newly created Address object to store it in an array. Suggestions include passing objects as parameters to the constructor instead of trying to reference them directly. The conversation also touches on the appropriate design of the AddressBook class, emphasizing that it should not duplicate instance variables from the other classes. Lastly, the user is exploring GUI integration issues related to extending JFrame while managing the AddressBook functionality.
Live4eva_2
Messages
28
Reaction score
0
Hi,


I have 4 classes:Person,Date,Address,AddressBook.

I want to create an array of address book objects.(ive never created an object array though).

What I have done is created Person,Date and Address classes.Lastly,my Address class
has a default constructor that looks something like this:

public class AddressBook
{
private static int noOfEntries = 0;
private static AddressBook[] entries = new AddressBook[500];


public Address()
{

Person temp = new Person();
Date aDate = new Date();
Address anAddress = new Address();
entries[noOfEntries] = ...


}​
}
This is the first time I've used constructors within another constructor.That seems to work fine though.As you can see I got stuck trying to reference the object that will be constructed.I tried using the keyword "this" but that doesn't seem to work.I just want to stick the object in the array at the position specified by noOfEntries.Can someone please help me out??

There is another small problem too though,usually I override the method toString() in my
classes in order to print it's data members.In this case though,I'm not sure how/where to define toString().
 
Physics news on Phys.org
Oh ya...I would have posted this in a programming forum but my usual haunt is offline for some reason.Besides,I am a Comp_Sci student...(relatively LOL)
 
you can't as far as I know, refer to the object beeing constructed by anything else that referring to its instance variables.

try to send the objects you want to put in your registers as parameters to the constructor instead. Or what is the structure of your program? What more constructors do you have etc?..
 
I'm no expert on Java but I have used "object oriented programming" in C++ and I see nothing peculiar in what you have done. From the title, "constructors within constructors", I thought you were defining a constructor for one object within a constructor for another object! That would be very peculiar. As it is, you are just declaring an instance of another object- that's common. I do NOT see where you are having a problem however, so I may be completely off.
 
My constructors are defined..I am just creating instances of the Person,Date and Address classes.

you can't as far as I know, refer to the object beeing constructed by anything else that referring to its instance variables.

try to send the objects you want to put in your registers as parameters to the constructor instead. Or what is the structure of your program? What more constructors do you have etc?..

Person has these instance variables:String firstName,String lastName
Date has these instance variables:int day,int month,int year
Address has these:String city,String country,String street,String zip

Does this mean that my addressBook class should have all of these as instance variables:

String firstName,String lastName
int day,int month,int year
String city,String country,String street,String zip
AddressBook[] book;
int noOfEntries;

So my constructor should look like this:

public AddressBook()
{
person pRef = new person()
firstName=pRef.getFirstName();
lastName=pRef.getLastName();

date dateRef = new date();
month = dateRef.getMonth();
day = dateRef.getDay();
year = dateRef.getYear();

address aRef = new address();
city = aRef.getCity();
country = aRef.getCountry();
street = aRef.getStreet();
zip = aRef.getZip();


}
??
 
OK!

that seemed to work.
Just need to find out why my address object is printing nulls...

thanx guys!
 
no your AdressBook class should not have the same instance variables as the Person, Date and Adress classes have. AdressBook should just have the instances that are relevant for AdressBook's. Like the number of entries (should not be static). Same holds for methods. Your program maybe works, but it is bad programming style and in the future you will run into problems etc.
 
oh..This is confusing.I'm doing this exercise for practice.It's out of DS Malik's Java Programming with data structures book.The question asks me to create those 4 classes.It didn't say that I must extend any classes though.The only other way I can think of is to use the clone() method...would that be acceptable?Because the chapter I'm working on uses clone() and compareTo() a lot so that may be the answer..

Also,I've started creating the GUI but unfortunately our lecturer skipped the chapter on JFrame.So I've only created one or two really simple GUI's.I realized now that I won't be able to use my addressBook class because the GUI will already extend JFrame.How should I work around that?
 
Last edited:
okay don't worry I got somebody helping me on another site
 

Similar threads

Replies
2
Views
2K
Replies
12
Views
2K
Replies
7
Views
3K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
1
Views
3K
Back
Top