How to call an object within the creation of another object

Asked

Viewed 71 times

6

I have a Person class and a Date class, the Person class creates a person the date class creates a date to use as the date of birth in the Person class, how do I create a date without having to use the full code in the middle of the person object, the code created so far is this:

public static void main(String[] args) {
        Data d = new Data(0, 0, 0);
        Pessoa p = new Pessoa("Miguel", "Rua D. Maria", "7400604", objeto_data, "Solteiro");

    }

1 answer

6


It is possible to create the direct date in the command, according to the code below:

Pessoa p = new Pessoa("Miguel", "Rua D. Maria", "7400604", new Data(0, 0, 0), "Solteiro");

Or create this date in the constructor of the Person class.

  • 1

    OK that’s just what I was forgetting, so I don’t need to create the reference ?

  • 1

    The command new creates the reference and passes directly to the method. This way you do not need to create the variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.