Could someone explain step by step how to get output?(reference type question) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Could someone explain step by step how to get output?(reference type question)

public class MyClass { public static void main(String[ ] args) { Person j; j = new Person("John"); j.setAge(20); celebrateBirthday(j); System.out.println(j.getAge());} static void celebrateBirthday(Person p) { p.setAge(p.getAge() + 1);}} public class Person { private String name; private int age; Person (String n) { this.name = n; } public int getAge() { return age;} public void setAge(int a) { this.age = a; }}

19th Oct 2017, 5:57 AM
oyl
1 Answer
+ 14
You create a Person object, then set it's age attribute to 20. You pass the Person object to the celebrateBirthday method, where you call the getAge method for the object, add 1 and then set the new value (21) to the age attribute of the Person object. Then you call getAge to print the current age. Output is 21.
5th Nov 2017, 9:19 PM
Tashi N
Tashi N - avatar