A topic is not so clear I will try to explain my question in an easy example. We know what all clients of a bank share money from a bank. So if the bank is bankrupt, no client could take a loan. Based on that point of view I will try to make a simple program.
Approach 1:
class Client {
private static int sharedMoney = 100;
void takeLoan(int amount) {
sharedMoney = sharedMoney - amount;
}
}
Approach 2:
class Client {
private SharedMoney sharedMoney;
public Client(SharedMoney sharedMoney) {
this.sharedMoney = sharedMoney;
}
void takeLoan(int amount) {
sharedMoney.setAmount(sharedMoney.getAmount() - amount);
}
}
class SharedMoney {
private int amount;
public SharedMoney(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
Of course, logic is not 100% covered, but I think I showed my point. Does anyone have a rule of thumb I should follow when coding? When should I use static when sharing something (money) and when should I pass reference in the constructor when again sharing the same thing (money)?