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

Time:03-08

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:

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

CodePudding user response:

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);
  •  Tags:  
  • java
  • Related