Last active
January 20, 2019 10:33
-
-
Save hamada147/1ddaed8724012e69444b8e1cd1454af4 to your computer and use it in GitHub Desktop.
pass by reference example.java for a medium article https://medium.com/@moussa.ahmed95/we-were-deceived-by-the-java-documentation-and-here-is-the-why-638bcc49f83c
This file contains hidden or 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
/** | |
* | |
* @author Ahmed Moussa | |
*/ | |
public class JavaApplication1 { | |
String name; | |
int age; | |
public JavaApplication1() { | |
name = "empty"; | |
age = 0; | |
} | |
@Override | |
public String toString() { | |
return "name: " + name + ", age: " + age; | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
JavaApplication1 x = new JavaApplication1(); | |
System.out.println(x.toString()); // name: empty, age: 0 | |
changeName(x); | |
System.out.println(x.toString()); // name: Ahmed Moussa, age: 0 | |
changeAge(x); | |
System.out.println(x.toString()); // name: Ahmed Moussa, age: 23 | |
} | |
public static void changeName(JavaApplication1 obj) { | |
obj.name = "Ahmed Moussa"; | |
} | |
public static void changeAge(JavaApplication1 obj) { | |
obj.age = 23; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment