0

I'm adding an instance variable to a class "Person" which is a reference type ("Date", which I have written a class for). In the constructor for my Person class, I am therefore trying to initialize the Date attribute using the constructor of the Date class, but I am unsure how to do this. Previously I have only ever initialized primitive types (or Strings), as seen below. This is a segment from my code. I'm unsure how to initialize "birthday" so that it uses the constructor of the Date class. Thanks!

public class Person {

/* Attribute declarations */
private String lastName;    // last name
private String firstName;   // first name
private String email;       // email address
private Date birthday;  // birth date

/**
 * Constructor initializes the person's name, email address, and birthday
 */
public Person(String firstName, String lastName, String email, Date birthday) {
    this.firstName = firstName;
    this.lastName = lastName;       
    this.email = email;
    this.birthday = ????

3 Answers 3

1

Are you saying you want to initialize this.birthday in the constructor of Person using the Date constructor? Then use the new keyword like this:

this.birthday = new Date(<arguments if any exist>);

new calls the constructor of an object. If that's the case, you do not need the Date birthday constructor argument for Person, unless you use it for something else.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought to do this, but I'm confused. Take the firstName variable for example. In the constructor, we have "this.firstName = firstName;", which is essentially giving the the firstName attribute (which was declared earlier) the value that is entered as a parameter (the "firstName" parameter), isn't it? So if I wrote this.birthday = new Date(); then how do the entered parameters for birthday get passed to it?
Well, then this.birthday = birthday; is what you are looking for. It works the same way like as for your example, this.firstName = firstName;, the birthday object will 'point' to the same object as the one passed by parameter. However be warned, like Joni said below, if something changes the birthday argument it changes the birthday in the class and vice versa.
Okay I think I understand. I wrote it like you originally suggested, and that correctly created the Date object according to my default Date parameters. But I also need to add another constructor to my Person code that will allow me to create a birthday that points to the parameters being passed in a new Person. You said this.birthday = birthday; which I understand would work, except that birthday is made up of three parameters: int year, int month, int day. So how can I set up a constructor that allows Person to accept int year, int month, int day and assign it to birthday?
You can have the constructor have int month, int year, int day as parameters, then do this: this.birthday = new Date(day, month, year);
Nevermind actually, I figured it out! I wrote this.birthday = new Date(year, month, day); which worked. Thanks for all of your help, Dynomyte and everyone else!
1

You can do this:

this.birthday = new Date(birthday.getTime());

This creates a copy of the date object. Since a Date can be modified it is dangerous to use the same object, which you'd be doing if you just copied the reference:

this.birthday = birthday;

That would allow the outside world to change your birthday without you knowing about it.

Comments

0

You can just simple

this.birthday = (Date) birthday.clone();

Why this way instead of ?

this.birthday = birthday;

Cause outsiders can modify your date object, and then they are modifying your internal structure and that is not good, breaks encapsulation.

Why this way instead of ?

this.birthday = new Date(birthday.getTime());

Date is not a final class what happen if Date object you pass is not a "true Date" and is a subclass , if you do this don't preserve internal structure of subclass, but when you cloning preserves the information, but this approach it depends on what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.