Java:Constructors within constructors

  • Context: Comp Sci 
  • Thread starter Thread starter Live4eva_2
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the implementation of constructors within a Java class structure involving multiple classes: Person, Date, Address, and AddressBook. Participants explore issues related to object creation, referencing constructed objects, and the design of the AddressBook class.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to create an array of AddressBook objects and expresses confusion about referencing the object being constructed within its own constructor.
  • Another participant suggests passing the objects as parameters to the constructor instead of trying to reference them directly.
  • A participant with experience in C++ comments that the approach taken does not seem unusual, but expresses uncertainty about the specific problem being faced.
  • Further elaboration on the instance variables of the Person, Date, and Address classes leads to a question about whether the AddressBook class should contain all these variables, which another participant disputes, advocating for better programming style.
  • One participant mentions that their Address object is printing null values, indicating a potential issue with object initialization.
  • Another participant raises the possibility of using the clone() method as a solution, referencing the context of their exercise from a textbook.
  • A participant expresses concern about GUI implementation, noting that their AddressBook class may conflict with extending JFrame.

Areas of Agreement / Disagreement

Participants express differing views on the design of the AddressBook class and how it should relate to the other classes. There is no consensus on the best approach to take regarding constructors and instance variables.

Contextual Notes

Some participants highlight potential issues with programming style and object initialization, but these concerns remain unresolved. The discussion also touches on the limitations of the current understanding of GUI implementation in relation to class structure.

Who May Find This Useful

Students and practitioners of Java programming, particularly those interested in object-oriented design and constructor usage.

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 address 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 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K