0

JAVA Reference type question pls help

could someone explain step by step how to get the output?please....https://code.sololearn.com/cMCDhvQZcgXd/?ref=app

19th Oct 2017, 2:06 PM
oyl
6 Antworten
+ 2
What output?
19th Oct 2017, 2:14 PM
Chriptus13
Chriptus13 - avatar
+ 2
So you made a Person and you made that person have 20 years with the method setAge(int) then when you call celebrateBirthday(Person) with the person you created as a parameter you increment that Person's age. Finally when you print her age you get 21 because 20+1=21.
19th Oct 2017, 2:21 PM
Chriptus13
Chriptus13 - avatar
+ 2
Oh p is the local variable to the method celebrateBirthday(Person) otherwise you would need to know the variable name each time you call that method so therefore you use p to refere to j inside celebrateBirthday method. About the second problem: you're setting the Person's age with the parameter (p.getAge()+1) where you call her age and add 1 so everytime you call celebrateBirthday you have to know the Person's age and you do that with the method getAge()
19th Oct 2017, 2:35 PM
Chriptus13
Chriptus13 - avatar
0
@André Martins when you run the code the output is 21.but I dont understand the code and how it derive that answer.
19th Oct 2017, 2:17 PM
oyl
0
@André Martins im confused with the p and the j....and also this p.setAge(p.getAge() + 1);
19th Oct 2017, 2:29 PM
oyl
0
@André Martins i have written 6 steps in the code to derive the output.can you help me check if my steps are correct.thankyou very much
19th Oct 2017, 3:29 PM
oyl