Java:Constructors within constructors

  • Comp Sci
  • Thread starter Live4eva_2
  • Start date
In summary, Person, Date, and Address classes have default constructors. AddressBook has a default constructor that assigns values to the instance variables firstName, lastName, month, day, year, city, country, and street. Person, Date, and Address classes have to send their objects as parameters to the AddressBook's constructor, but AddressBook doesn't have to send any objects. The AddressBook's constructor should look like this: AddressBook() { person pRef = new person(); firstName=pRef.getFirstName(); lastName=pRef.getLastName(); dateRef = new date(); month=dateRef.getMonth(); day=dateRef.getDay
  • #1
Live4eva_2
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().
 
Physics news on Phys.org
  • #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)
 
  • #3
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?..
 
  • #4
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.
 
  • #5
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();


}
??
 
  • #6
OK!

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

thanx guys!
 
  • #7
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.
 
  • #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() 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:
  • #9
okay don't worry I got somebody helping me on another site
 

What is a constructor in Java?

A constructor in Java is a special method that is used to initialize objects of a class. It is called automatically when an object is created and is used to set initial values for the object's attributes.

What is the purpose of using constructors within constructors?

Constructors within constructors, also known as constructor chaining, allows for more flexibility in object initialization. It allows one constructor to call another constructor in the same class, reducing the need for duplicate code and making the code more maintainable.

Can a constructor call itself in Java?

Yes, a constructor can call itself in Java. This is known as recursive constructor invocation and is achieved by using the this() keyword inside the constructor.

What is the difference between a default constructor and a parameterized constructor?

A default constructor is a constructor that has no parameters and is automatically provided by Java if no other constructor is specified. A parameterized constructor, on the other hand, allows for the passing of parameters to initialize the object's attributes.

Can a constructor be declared as private in Java?

Yes, a constructor can be declared as private in Java. This is often used in singleton design pattern where only one instance of a class is allowed. A private constructor prevents other classes from creating new instances of the class.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
946
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
Back
Top