Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make an object that has a another object as a parameter in JAVA

So I have 2 constructors

one for the object MyDate:

public MyDate(int month, int day, int year) {
    this.month = month;
    this.day = day;
    this.year = year;

and one for the object Person:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public Person(String name, MyDate dob) {
    this.name = name;
    this.dob = dob;

so how do I make a new Person object in my main method?

Person p1 = new Person(?????);

>Solution :

The answer is:

Person p1 = new Person("John", new MyDate(3, 8, 2022));

You can also instantiate a date first, separately, then pass the variable to the constructor for the Person object:

MyDate endOfCovid = new MyDate(12, 25, 9999);
Person someGuy = new Person("John", endOfCovid);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading