1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java:Constructors within constructors

  1. Mar 15, 2008 #1

    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().
  2. jcsd
  3. Mar 15, 2008 #2
    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)
  4. Mar 15, 2008 #3


    User Avatar
    Science Advisor
    Homework Helper

    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?..
  5. Mar 15, 2008 #4


    User Avatar
    Staff Emeritus
    Science Advisor

    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.
  6. Mar 15, 2008 #5
    My constructors are defined..I am just creating instances of the Person,Date and Address classes.

    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()

    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();

  7. Mar 15, 2008 #6

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

    thanx guys!
  8. Mar 15, 2008 #7


    User Avatar
    Science Advisor
    Homework Helper

    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.
  9. Mar 15, 2008 #8
    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: Mar 15, 2008
  10. Mar 16, 2008 #9
    okay dont worry I got somebody helping me on another site
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Java:Constructors within constructors