I create a new Account object with 10 as initial balance, then I set the object variable to null and try to add money to the account using said null reference and it still works. The following code prints 20, it doesn’t matter if I set acct to null or not.
public class Main {
public static void main(String[] args) {
Account acct = new Account(10);
acct = null;
acct.deposit(10);
System.out.println(acct.getBalance()); //prints 20
}
}
Shouldn’t I get an error that the object is not reachable using acct variable?
1