Created
July 27, 2014 07:14
-
-
Save adohe-zz/372de02fca79ca888b99 to your computer and use it in GitHub Desktop.
Arguments - Call by Value(Java Version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ado.java; | |
public class TestRef { | |
public static void main(String[] args) | |
{ | |
ValueObject vo1 = new ValueObject("A", 1); | |
System.out.println("after vo1: " + vo1.getName()); //=A | |
changeValue1(vo1); | |
System.out.println("after changeValue1: " + vo1.getName()); | |
//=A1, changed | |
changeValue2(vo1); | |
System.out.println("after changeValue2: " + vo1.getName()); | |
ValueObject v2 = new ValueObject("C", 3); | |
ValueObject v3 = new ValueObject("D", 4); | |
v2 = v3; | |
System.out.println("after v2=v3"); | |
System.out.println("v2= "+ v2.getName());//=D | |
System.out.println("v3= "+ v3.getName());//=D | |
v3.setName("C1"); | |
System.out.println("after v3 setnameTo C1"); | |
System.out.println("vo2= "+ v2.getName());//=C1 | |
System.out.println("vo3= "+ v3.getName());//=C1 | |
} | |
private static void changeValue1(ValueObject vo1) { | |
vo1.setName("A1"); | |
} | |
private static void changeValue2(ValueObject vo1) { | |
vo1 = new ValueObject("B", 2); | |
System.out.println("inside changeValue2: "+ vo1.getName()); | |
} | |
} | |
class ValueObject { | |
public ValueObject() {} | |
public ValueObject(String name, int id) | |
{ | |
this.name = name; | |
this.id = id; | |
} | |
private String name; | |
private int id; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
understand the variable itself and the value of variable.....