I'm trying to understand how parameters are passed in Java. For example, I have the following code:
class Runner{
public static void main(String[] args)
{
Integer test = new Integer(20);
updateObject(test);
System.out.println(test);
}
public static void updateObject(Integer test)
{
test = 50;
}
}
It prints "20".
However, if I use my own class instead of Integer like this:
import java.util.*;
class Test {
int num;
Test(int x){
num = x;
}
Test(){
num = 0;
}
}
class Runner{
public static void main(String[] args)
{
Test test = new Test(20);
updateObject(test);
System.out.println(test.num);
}
public static void updateObject(Test test)
{
test.num = 50;
}
}
In this case println prints "50".
Why in the 1st case my parameter was not changed but it has been changed in the 2nd case?