Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. //Author: bernblend 11/02/16
  2. /*This program shows a variable being passed by reference
  3. (the change is being done to the variable, not just the
  4. value, like when 'passing by value')*/
  5. public class Person{
  6. private String name;
  7. private int age;
  8.  
  9. Person(String n){
  10. this.name = n;
  11. }
  12.  
  13. public int getAge(){
  14. return age;
  15. }
  16.  
  17. public void setAge(int a){
  18. this.age = a;
  19. }
  20.  
  21. }
  22.  
  23.  
  24.  
  25.  
  26. class MyClass{
  27. public static void main(String[] args){
  28. Person j;
  29. j = new Person("John");
  30. j.setAge(20);
  31. celebrateBirthday(j);
  32. System.out.println(j.getAge());
  33. }
  34. static void celebrateBirthday(Person p){
  35. p.setAge(p.getAge()+1); //get the age, increment, then set it.
  36. }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement