Advertisement
yo2man

Tutorial 48 Passing by Value

May 26th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. //Tutorial 48 Passing by Value
  2. //a bit harder than simple but not too hard
  3.  
  4. //---------------------------------------------------------------------------------------------------------------------------------------
  5. //Person.java
  6. public class Person {
  7.     private String name;
  8.    
  9.    
  10.     public Person(String name) {
  11.         this.name = name;
  12.     }
  13.    
  14.     public String getName(){
  15.         return name;
  16.     }
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.    
  21.     @Override
  22.     public String toString() {
  23.         return "Person [name=" + name + "]";
  24.     }
  25. }
  26.  
  27. //---------------------------------------------------------------------------------------------------------------------------------------
  28. //App.java
  29. public class App {
  30.    
  31.     public static void main(String[] args) {
  32.         App app = new App();
  33.        
  34.        
  35.         //==============================================
  36.  
  37.         int value = 7;
  38.             System.out.println("1. Value is: " + value);
  39.        
  40.         app.show(value);
  41.        
  42.             System.out.println("4. Value is: " + value);
  43.  
  44.         //==============================================
  45.         System.out.println();
  46.        
  47.         Person person = new Person("Bob");
  48.             System.out.println("1. Person is: " + person);
  49.            
  50.         app.show(person);
  51.        
  52.         System.out.println("4. Person is: " + person);
  53.     }
  54.    
  55.     public void show(int value) {
  56.             System.out.println("2. Value is: " + value);
  57.        
  58.         value = 8;
  59.        
  60.             System.out.println("3. Value is: " + value);
  61.     }
  62.    
  63.     public void show(Person person){ //method overloading, you can have the method of the same name as long as the have different stuff in the (  )
  64.         System.out.println("2. Person is: " + person);
  65.        
  66.         person = new Person("mike");
  67.        
  68.         System.out.println("3. Person is: " + person);
  69.     }
  70. }
  71.  
  72. //---------------------------------------------------------------------------------------------------------------------------------------
  73. /*Run time results:
  74. 1. Value is: 7
  75. 2. Value is: 7
  76. 3. Value is: 8
  77. 4. Value is: 7
  78.  
  79. 1. Person is: Person [name=Bob]
  80. 2. Person is: Person [name=Bob]
  81. 3. Person is: Person [name=mike]
  82. 4. Person is: Person [name=Bob]
  83. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement