Why create a temp here?

25 views Asked by At

what is the purpose of creating a temp here?

void printAcc(int identifier) {
        
        User2 temp = um.userList[identifier];
        System.out.println("=====================");
        System.out.println("ID : " + temp.id);
        System.out.println("=====================");
        for (int i = 0; i < temp.accCnt; i++) {
            System.out.println("accNumber: " + temp.acc[i].accNumber + "/ money: " + temp.acc[i].money);
        }
        System.out.println("=================================\n");
    }
1

There are 1 answers

0
Clashsoft On

The variable can be there so you don't have to repeat the expression um.userList[identifier] all over the method. This is helpful if you ever need to change the way this value is retrieved (e.g. userList is changed to be a List instead of an array)

If you give it a better name than temp, e.g. user or accountHolder, it even serves as documentation what the variable does.