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; }}
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.