• Support PF! Buy your school textbooks, materials and every day products Here!

Java:Constructors within constructors

  • Comp Sci
  • Thread starter Live4eva_2
  • Start date
28
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().
 

Answers and Replies

28
0
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)
 
malawi_glenn
Science Advisor
Homework Helper
4,782
22
you cant as far as I know, refer to the object beeing constructed by anything else that refering 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?..
 
HallsofIvy
Science Advisor
Homework Helper
41,732
893
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.
 
28
0
My constructors are defined..I am just creating instances of the Person,Date and Address classes.

you cant as far as I know, refer to the object beeing constructed by anything else that refering 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();


}
??
 
28
0
OK!!

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

thanx guys!
 
malawi_glenn
Science Advisor
Homework Helper
4,782
22
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.
 
28
0
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() alot 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 realised now that I wont be able to use my addressBook class because the GUI will already extend JFrame.How should I work around that?
 
Last edited:
28
0
okay dont worry I got somebody helping me on another site
 

Related Threads for: Java:Constructors within constructors

  • Last Post
Replies
2
Views
556
  • Last Post
Replies
0
Views
765
Replies
3
Views
1K
  • Last Post
Replies
0
Views
1K
Replies
2
Views
3K
Replies
7
Views
998
Replies
2
Views
1K
  • Last Post
Replies
7
Views
6K
Top