0

Why celebrateBirthday() is declared in MyClass??

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

3rd Jan 2017, 8:52 AM
Mohd Kaleem Khan
Mohd Kaleem Khan - avatar
1 Resposta
0
to increase number of years that person has. p.getage() - gets actual age of person. p.getage()+1 - adds one to value. p.setage(p.getage() +1) - set actual age in variable age. read about getters and setters in java
7th Jan 2017, 12:18 AM
Stefan Milosavljevic
Stefan Milosavljevic - avatar