I am starting to learn Java and I am struggling trying to understand why in this situation, the account money doesn't change to 5.
Trying with code I have realized that inside the method manipulate()
the money of c1 actually changes but when returns to the main, it just increases and doesn't get the value of c2 (guess it is something about memory directions).
I am little familiarized with C and I understand it must be something like using a parameter with or without pointer but as this is OOP, I cant understand it. Could someone explain me easily?
public class Account {
private int money;
public Account(int money){
this.money=money;
}
public static void manipulate(Account c1){
c1.add(120);
Account c2 = new Account(5);
c1 = c2;
}
public void add(int money){
this.money += money;
}
public int actual_money(){
return this.money;
}
public static void main(String args[]) {
Account c1 = new Account(100);
manipulate(c1);
System.out.println("Money = " + c1.actual_money()); //prints 220
}
}